java/struts/Servlet文件下載與ftp文件上傳下載

1.前端代碼前端

使用超連接到Struts的Action或Servletapache

<a target="_blank" href="ftpFileAction!downloadFile.action?transUrl=ftp://10.0.2.1/tazi/a.xml">請點擊下載</a>tomcat

2.後臺代碼 Action或Servlet服務器

 

String s1=transUrl; // transUrl是前臺接受的參數,get接受的參數tomcat一概使用iso-8859-1編碼app

transUrl=new String(transUrl.getBytes("ISO-8859-1"),"utf-8");//程序中要使用的文件名,必須轉換爲gbk編碼
s1=s1.substring(6);
s1=s1.substring(s1.indexOf("/"));
String filename=s1.substring(s1.lastIndexOf("/")+1);
String filepath=s1.substring(0,s1.lastIndexOf("/"));測試

 
 

response.setCharacterEncoding("gbk");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=\"" + new String(filename.getBytes("gbk"),"ISO-8859-1")+ "\"");//response文件頭中定義的filename包含的中文必須是原始的ISO-8859-1編碼。編碼

 
 

boolean suc=false;
suc=downFileStream("10.0.2.1", 21,"myUser","myPwd",
filepath, filename, os);//downFileStream是一個下載ftp文件到文件流的方法
if(!suc){
response.reset();
response.setCharacterEncoding("gbk");
PrintWriter writer=new PrintWriter(os);
writer.write("指定的文件不存在!");
writer.close();
}else{
os.close();
};
url

 

3.ftp文件下載處理spa

注意這裏引入的包是.net

org.apache.commons.net.ftp.*

  (1).下載到本地

import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public static boolean downFile(String url, // FTP服務器hostname
        int port,// FTP服務器端口
        String username, // FTP登陸帳號
        String password, // FTP登陸密碼
        String remotePath,// FTP服務器上的相對路徑
        String fileName,// 要下載的文件名
        String localPath// 下載後保存到本地的路徑
) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port); // 若是採用默認端口,可使用ftp.connect(url)的方式直接鏈接FTP服務器
        ftp.login(username, password);// 登陸
        reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(remotePath);// 轉移到FTP服務器目錄
        FTPFile[] fs = ftp.listFiles(); boolean ftpFileExist=false; for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { ftpFileExist=true; File dir = new File(localPath); if(!dir.exists()){ dir.mkdirs(); } File localFile = new File(localPath + File.separator + ff.getName()); OutputStream is = new FileOutputStream(localFile,true); ftp.setBufferSize(1024); if(ftp.setFileType(FTP.BINARY_FILE_TYPE)){ ftp.retrieveFile(ff.getName(), is); is.close(); } } } ftp.logout(); if(ftpFileExist){ success = true; } } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }

 

  (2).下載到文件輸出流 OutputStream

public static boolean downFileStream(String url, // FTP服務器hostname
        int port,// FTP服務器端口
        String username, // FTP登陸帳號
        String password, // FTP登陸密碼
        String remotePath,// FTP服務器上的相對路徑
        String fileName,// 要下載的文件名
 OutputStream os ) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port); // 若是採用默認端口,可使用ftp.connect(url)的方式直接鏈接FTP服務器
        ftp.login(username, password);// 登陸
        reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return false; } ftp.changeWorkingDirectory(remotePath);// 轉移到FTP服務器目錄
        FTPFile[] fs = ftp.listFiles(); boolean ftpFileExist=false; for (FTPFile ff : fs) { String ffName=new String(ff.getName().getBytes("ISO-8859-1"),"gbk"); if (ffName.equals(fileName)) { ftpFileExist=true; ftp.setBufferSize(1024); if(ftp.setFileType(FTP.BINARY_FILE_TYPE)){ ftp.retrieveFile(ff.getName(), os); } } } ftp.logout(); if(ftpFileExist){ success = true; } } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }

 4.ftp文件上傳

public static boolean uploadFile(String url,// FTP服務器hostname
        int port,// FTP服務器端口
        String username, // FTP登陸帳號
        String password, // FTP登陸密碼
        String path, // FTP服務器保存目錄
        String filename, // 上傳到FTP服務器上的文件名
        InputStream input // 輸入流
) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        ftp.connect(url, port);// 鏈接FTP服務器
        // 若是採用默認端口,可使用ftp.connect(url)的方式直接鏈接FTP服務器
        ftp.login(username, password);// 登陸
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);

        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
    return success;
}

上傳時中文名處理,要轉換爲

ISO-8859-1編碼
public static void main(String[] args) throws Exception{
        FtpUtil f= new FtpUtil();
        File file=new File("D:/測試.xml");
        InputStream isInputStream=new FileInputStream(file);
        String filename=new String("中文.xml".getBytes("gbk"),"ISO-8859-1");
        f.uploadFile("10.7.0.15", 21, "dd", "dd", "/tazi",
                filename, isInputStream);
        
    }
相關文章
相關標籤/搜索