java在浏览器上获取FTP读文件路径

java在浏览器上获取FTP读文件路径,第1张

问一下,你是想做ftp上传下载么?

首先你需要安装一个ftp服务端程序,启动起来,然后下载一个ftp客户端程序,测试能不能连接,首先这一块儿需要测试通过。

代码ftp上传下载

21 上传代码:

import javaioFile;

import javaioFileInputStream;

import orgapachecommonsnetftpFTPClient;

import orgapachecommonsnetftpFTPReply;

public class test {

private FTPClient ftp;

/

@param path 上传到ftp服务器哪个路径

@param addr 地址

@param port 端口号

@param username 用户名

@param password 密码

@return

@throws Exception

/

private boolean connect(String path,String addr,int port,String username,String password) throws Exception {

boolean result = false;

ftp = new FTPClient();

int reply;

ftpconnect(addr,port);

ftplogin(username,password);

ftpsetFileType(FTPClientBINARY_FILE_TYPE);

reply = ftpgetReplyCode();

if (!FTPReplyisPositiveCompletion(reply)) {

ftpdisconnect();

return result;

}

ftpchangeWorkingDirectory(path);

result = true;

return result;

}

/

@param file 上传的文件或文件夹

@throws Exception

/

private void upload(File file) throws Exception{

if(fileisDirectory()){

ftpmakeDirectory(filegetName());

ftpchangeWorkingDirectory(filegetName());

String[] files = filelist();

for (int i = 0; i < fileslength; i++) {

File file1 = new File(filegetPath()+"\\"+files[i] );

if(file1isDirectory()){

upload(file1);

ftpchangeToParentDirectory();

}else{

File file2 = new File(filegetPath()+"\\"+files[i]);

FileInputStream input = new FileInputStream(file2);

ftpstoreFile(file2getName(), input);

inputclose();

}

}

}else{

File file2 = new File(filegetPath());

FileInputStream input = new FileInputStream(file2);

ftpstoreFile(file2getName(), input);

inputclose();

}

}

public static void main(String[] args) throws Exception{

test t = new test();

tconnect("", "localhost", 21, "yhh", "yhhazr");

File file = new File("e:\\uploadify");

tupload(file);

}

}

22 下载代码

这里没有用到filter,如果用filter就可以过滤想要的文件。

public class Ftp {

   /

    @param args

    /

   public static void main(String[] args) {

       // TODO Auto-generated method stub

       Ftp ftp = new Ftp();

       String hostname = ">

同意楼上的分析,另外我想提个建议,代码为了便于阅读可以试着分步骤写。

就先定义filename和filepath,然后你在后续的 *** 作中想要出现在设定的某一区域中

就可以直接使用filename,将要上传的文件名是 itsgetIPTimeStampRand()+""+itemgetName()split("\\")[1]

由于 *** 作系统和浏览器为了安全性,浏览器点击上传文件后,浏览器是不知道真实路径的,也就是说浏览器是不知道E:\我的文件夹\文件1xlsx的位置的。

文件的后缀可以在前台通过jquery的方式获取到上传文件的文件名称,然后通过字符串截取就可以了。

上传文件时,需要给SmartUpload传一个上传文件的保存地址,一般都用一个String来表示,使用request来获得。比如,你要把文件保存在项目路径下的upload文件夹,刚:

String

path

=

requestgetRealPath("upload");//获取upload文

//夹的绝对路径。

//得到:c://项目名/upload/

然后再拼上你的文件名,比如你上传一个叫testtxt的文件,则全部的URL为:

path

+

fileName

=

"c:///项目名/upload/"+"texttxt";

在实际的编程中,其实数据库只需要保存fileName就可以了,path作为配置,每次系统加载时再去读取,这样做的好处是:系统的环境变化时,只需要修改配置文件就能够搞定,而且不会重复保存多余的路径(因为路径都是一样的)

以上就是关于java在浏览器上获取FTP读文件路径全部的内容,包括:java在浏览器上获取FTP读文件路径、java request 怎么查上传文件、JAVA如何获取上传后的文件名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9325344.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存