前几天 有个需求就是上传文件的时候,本地存一份,其他服务器也保存一份,于是就研究了一下,功能只实现了上传文件不能上传文件夹,
这里主要用到了 ftp服务器软件 网上有很多我用的是 serv-u,操作比较简单很容易像我这样的小菜使用
服务器下载地址
点击打开链接 绝对安全无病毒
实现类代码
package com.core.haction; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import com.core.action.BaseAction; public class ShangChuanFtp extends BaseAction<Model> { public ShangChuanFtp() { super(Model.class); // TODO Auto-generated constructor stub } /** * */ private static final long serialVersionUID = 1L; 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; ftp.connect(addr, port);//连接ftp服务器 ftp.login(username, password);//登录ftp ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(path); result = true; return result; } /** * * @param file * 上传的文件或文件夹 * * 代码我是从网上找的,在使用过程中出现了,中文文件名称不能上传,后来自己改变了一下编码 * @throws Exception */ private void upload(File file) throws Exception { if (file.isDirectory()) { // System.out.println(file.isDirectory()+"\n"+file.getName()); ftp.makeDirectory(new String(file.getName().getBytes("utf-8"),"8859_1"));//给文件名转换编码 //System.out.println(file.getName()); ftp.changeWorkingDirectory(new String(file.getName().getBytes("utf-8"),"8859_1")); String[] files = file.list(); for (int i = 0; i < files.length; i++) { File file1 = new File(file.getPath() + "\\" + files[i]); if (file1.isDirectory()) { upload(file1); ftp.changeToParentDirectory(); } else { File file2 = new File(file.getPath() + "\\" + files[i]); FileInputStream input = new FileInputStream(file2); ftp.storeFile(new String(file2.getName().getBytes("utf-8"),"8859_1"), input); input.close(); } } } else { File file2 = new File(file.getPath()); System.out.println(file2.getName()); FileInputStream input = new FileInputStream(file2); ftp.storeFile(new String(file2.getName().getBytes("utf-8"),"8859_1"), input); input.close(); } } // public static void main(String[] args) throws Exception{//本地测试方法 // ShangChuanFtp t = new ShangChuanFtp(); // t.connect("", "远程服务器的Ip地址", 21, "ftp的登录名", "ftp的登录密码"); // File file = new File("d:\\webapps");//要上传的文件地址 // t.upload(file); // System.out.println("上传完成"); // } /** * * @param file * 以下代码,住web页面用的,strut2的语法大家应该都会知道 * @throws IOException */ private File pphoto; private String pphotoFileName; private String pphotoFileContentType; private static final String filePath = "/while/photo";//上传文件到本地服务器的路径 private String textfield; public String scftp() throws IOException { PrintWriter out = getResponse().getWriter(); try { String fileUrl = null; if (pphoto != null) { //自己封装的上传本地服务器的方法fileUrl是方法的返回值我这里是返回的文件名称 fileUrl = this.saveFile(pphoto, pphotoFileName, filePath,false); System.out.println("3:" + this.getSession().getAttribute("dir")); //t.connect("", "远程服务器的Ip地址", 21, "ftp的登录名", "ftp的登录密码");21是端口号 connect("", "000.000.000.000", 21, "", ""); //this.getSession().getAttribute("dir")这个是获取的上传到本地服务器的路径,用了个懒办法,在上传方法我存到session里面,这边获取的 //fileUrl是你上传的那个文件名 File file = new File(this.getSession().getAttribute("dir")+"/"+fileUrl); upload(file); } else { out.print("0"); } } catch (Exception e) { // TODO Auto-generated catch block${ctx }/json/scftp.action System.out.println(e.getMessage()); } out.print("1"); return null; } public String getTextfield() { return textfield; } public void setTextfield(String textfield) { this.textfield = textfield; } public File getPphoto() { return pphoto; } public void setPphoto(File pphoto) { this.pphoto = pphoto; } public String getPphotoFileName() { return pphotoFileName; } public void setPphotoFileName(String pphotoFileName) { this.pphotoFileName = pphotoFileName; } public String getPphotoFileContentType() { return pphotoFileContentType; } public void setPphotoFileContentType(String pphotoFileContentType) { this.pphotoFileContentType = pphotoFileContentType; } }