java中ftp文件上传和中文乱码解决
来源:互联网 发布:淘宝只退款不退货教程 编辑:程序博客网 时间:2024/06/05 22:39
见:http://blog.csdn.net/cr135810/article/details/21535637
前几天 有个需求就是上传文件的时候,本地存一份,其他服务器也保存一份,于是就研究了一下,功能只实现了上传文件不能上传文件夹,
这里主要用到了 ftp服务器软件 网上有很多我用的是 serv-u,操作比较简单很容易像我这样的小菜使用
服务器下载地址
点击打开链接 绝对安全无病毒
实现类代码
- <pre code_snippet_id="244843" snippet_file_name="blog_20140319_4_6086968" name="code" class="java">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);
-
- }
-
-
-
-
- private static final long serialVersionUID = 1L;
- private FTPClient ftp;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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.login(username, password);
- ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
- reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- return result;
- }
- ftp.changeWorkingDirectory(path);
- result = true;
- return result;
- }
-
-
-
-
-
-
-
-
-
- private void upload(File file) throws Exception {
- if (file.isDirectory()) {
-
- ftp.makeDirectory(new String(file.getName().getBytes("utf-8"),"8859_1"));
-
- 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();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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 = this.saveFile(pphoto, pphotoFileName, filePath,false);
- System.out.println("3:" + this.getSession().getAttribute("dir"));
-
- connect("", "000.000.000.000", 21, "", "");
-
-
- File file = new File(this.getSession().getAttribute("dir")+"/"+fileUrl);
- upload(file);
- } else {
- out.print("0");
- }
-
-
- } catch (Exception e) {
-
- 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;
- }
-
- }