java中ftp文件上传和中文乱码解决

来源:互联网 发布:淘宝只退款不退货教程 编辑:程序博客网 时间:2024/06/05 22:39

见:http://blog.csdn.net/cr135810/article/details/21535637



前几天 有个需求就是上传文件的时候,本地存一份,其他服务器也保存一份,于是就研究了一下,功能只实现了上传文件不能上传文件夹,

这里主要用到了 ftp服务器软件 网上有很多我用的是 serv-u,操作比较简单很容易像我这样的小菜使用

服务器下载地址

点击打开链接           绝对安全无病毒

实现类代码

[java] view plain copy
  1. <pre code_snippet_id="244843" snippet_file_name="blog_20140319_4_6086968" name="code" class="java">package com.core.haction;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.PrintWriter;  
  7. import org.apache.commons.net.ftp.FTPClient;  
  8. import org.apache.commons.net.ftp.FTPReply;  
  9. import com.core.action.BaseAction;  
  10.   
  11. public class ShangChuanFtp extends BaseAction<Model> {  
  12.   
  13.     public ShangChuanFtp() {  
  14.         super(Model.class);  
  15.         // TODO Auto-generated constructor stub  
  16.     }  
  17.   
  18.     /** 
  19.      *  
  20.      */  
  21.     private static final long serialVersionUID = 1L;  
  22.     private FTPClient ftp;  
  23.   
  24.     /** 
  25.      *  
  26.      * @param path 
  27.      *            上传到ftp服务器哪个路径下 
  28.      * @param addr 
  29.      *            地址 
  30.      * @param port 
  31.      *            端口号 
  32.      * @param username 
  33.      *            用户名 
  34.      * @param password 
  35.      *            密码 
  36.      * @return 
  37.      * @throws Exception 
  38.      */  
  39.     private boolean connect(String path, String addr, int port,  
  40.             String username, String password) throws Exception {  
  41.         boolean result = false;  
  42.         ftp = new FTPClient();  
  43.         int reply;  
  44.         ftp.connect(addr, port);//连接ftp服务器  
  45.         ftp.login(username, password);//登录ftp  
  46.         ftp.setFileType(FTPClient.BINARY_FILE_TYPE);  
  47.         reply = ftp.getReplyCode();  
  48.         if (!FTPReply.isPositiveCompletion(reply)) {  
  49.             ftp.disconnect();  
  50.             return result;  
  51.         }  
  52.         ftp.changeWorkingDirectory(path);  
  53.         result = true;  
  54.         return result;  
  55.     }  
  56.   
  57.     /** 
  58.      *  
  59.      * @param file 
  60.      *            上传的文件或文件夹 
  61.      *             
  62.      *            代码我是从网上找的,在使用过程中出现了,中文文件名称不能上传,后来自己改变了一下编码 
  63.      * @throws Exception 
  64.      */  
  65.     private void upload(File file) throws Exception {  
  66.         if (file.isDirectory()) {  
  67.             // System.out.println(file.isDirectory()+"\n"+file.getName());  
  68.             ftp.makeDirectory(new String(file.getName().getBytes("utf-8"),"8859_1"));//给文件名转换编码  
  69.             //System.out.println(file.getName());  
  70.             ftp.changeWorkingDirectory(new String(file.getName().getBytes("utf-8"),"8859_1"));  
  71.             String[] files = file.list();  
  72.             for (int i = 0; i < files.length; i++) {  
  73.                 File file1 = new File(file.getPath() + "\\" + files[i]);  
  74.                 if (file1.isDirectory()) {  
  75.                     upload(file1);  
  76.                     ftp.changeToParentDirectory();  
  77.                 } else {  
  78.                     File file2 = new File(file.getPath() + "\\" + files[i]);  
  79.                     FileInputStream input = new FileInputStream(file2);  
  80.                     ftp.storeFile(new String(file2.getName().getBytes("utf-8"),"8859_1"), input);  
  81.                     input.close();  
  82.                 }  
  83.             }  
  84.         } else {  
  85.             File file2 = new File(file.getPath());  
  86.             System.out.println(file2.getName());  
  87.             FileInputStream input = new FileInputStream(file2);  
  88.             ftp.storeFile(new String(file2.getName().getBytes("utf-8"),"8859_1"), input);  
  89.             input.close();  
  90.         }  
  91.     }  
  92.   
  93.     // public static void main(String[] args) throws Exception{//本地测试方法  
  94.     // ShangChuanFtp t = new ShangChuanFtp();  
  95.     // t.connect("", "远程服务器的Ip地址", 21, "ftp的登录名", "ftp的登录密码");  
  96.     // File file = new File("d:\\webapps");//要上传的文件地址  
  97.     // t.upload(file);  
  98.     // System.out.println("上传完成");  
  99.     // }  
  100.     /** 
  101.      *  
  102.      * @param file 
  103.      *          以下代码,住web页面用的,strut2的语法大家应该都会知道   
  104.      * @throws IOException 
  105.      */  
  106.     private File pphoto;  
  107.     private String pphotoFileName;  
  108.     private String pphotoFileContentType;  
  109.     private static final String filePath = "/while/photo";//上传文件到本地服务器的路径  
  110.     private String textfield;  
  111.   
  112.     public String scftp() throws IOException {  
  113.         PrintWriter out = getResponse().getWriter();  
  114.           
  115.         try {  
  116.               
  117.             String fileUrl = null;  
  118.             if (pphoto != null) {  
  119.                 //自己封装的上传本地服务器的方法fileUrl是方法的返回值我这里是返回的文件名称  
  120.                 fileUrl = this.saveFile(pphoto, pphotoFileName, filePath,false);  
  121.                 System.out.println("3:" + this.getSession().getAttribute("dir"));  
  122.                 //t.connect("", "远程服务器的Ip地址", 21, "ftp的登录名", "ftp的登录密码");21是端口号  
  123.                 connect("""000.000.000.000"21"""");  
  124.                 //this.getSession().getAttribute("dir")这个是获取的上传到本地服务器的路径,用了个懒办法,在上传方法我存到session里面,这边获取的  
  125.                 //fileUrl是你上传的那个文件名  
  126.                 File file = new File(this.getSession().getAttribute("dir")+"/"+fileUrl);  
  127.                 upload(file);  
  128.             } else {  
  129.                 out.print("0");  
  130.             }  
  131.               
  132.   
  133.         } catch (Exception e) {  
  134.             // TODO Auto-generated catch block${ctx }/json/scftp.action  
  135.             System.out.println(e.getMessage());  
  136.         }  
  137.   
  138.         out.print("1");  
  139.         return null;  
  140.     }  
  141.   
  142.     public String getTextfield() {  
  143.         return textfield;  
  144.     }  
  145.   
  146.     public void setTextfield(String textfield) {  
  147.         this.textfield = textfield;  
  148.     }  
  149.   
  150.     public File getPphoto() {  
  151.         return pphoto;  
  152.     }  
  153.   
  154.     public void setPphoto(File pphoto) {  
  155.         this.pphoto = pphoto;  
  156.     }  
  157.   
  158.     public String getPphotoFileName() {  
  159.         return pphotoFileName;  
  160.     }  
  161.   
  162.     public void setPphotoFileName(String pphotoFileName) {  
  163.         this.pphotoFileName = pphotoFileName;  
  164.     }  
  165.   
  166.     public String getPphotoFileContentType() {  
  167.         return pphotoFileContentType;  
  168.     }  
  169.   
  170.     public void setPphotoFileContentType(String pphotoFileContentType) {  
  171.         this.pphotoFileContentType = pphotoFileContentType;  
  172.     }  
  173.   
  174. }

阅读全文
0 0