Java中如何实现FTP文件上传

  1 package com.ztesoft.ibss.ct.bo.recruit;
  2 
  3 import com.powerise.ibss.util.DBUtil;
  4 import com.ztesoft.ibss.common.util.SqlMapExe;
  5 import com.ztesoft.ibss.inf.framework.dao.SqlExe;
  6 import org.apache.commons.fileupload.FileItem;
  7 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  8 import org.apache.commons.fileupload.servlet.ServletFileUpload;
  9 import javax.servlet.ServletException;
 10 import javax.servlet.http.HttpServlet;
 11 import javax.servlet.http.HttpServletRequest;
 12 import javax.servlet.http.HttpServletResponse;
 13 import java.io.*;
 14 import java.sql.Connection;
 15 import java.util.List;
 16 
 17 /**
 18  * @Author zgp
 19  * @Since 2021 -03 -29 15 :16
 20  * @Description 招聘活动,pdf简历文件上传到服务器
 21  */
 22 public class RecruitActivityUpPdf extends HttpServlet {
 23 
 24     public RecruitActivityUpPdf() {
 25         super();
 26     }
 27 
 28     //Ftp主机名
 29     private String ftpHost = "134.224.165.193";
 30     //Ftp用户名
 31     private String ftpUserName = "wgzx";
 32     //Ftp密码
 33     private String ftpPassword = "5Z9k%ppNrsuTFlh3";
 34     //Ftp的端口号,默认21
 35     private int ftpPort = 21;
 36     //Ftp服务器的文件路径
 37     private String ftpPath = "/home/weblogic/Oracle/Middleware/user_projects/domains/APPdomain/upload/CtWeb/resume_pdf/";
 38     //上传文件名称
 39     //private String fileName = "Teacher.pdf";
 40 
 41     /*//Ftp主机名
 42     private String ftpHost = "134.225.67.7";
 43     //Ftp用户名
 44     private String ftpUserName = "yzf";
 45     //Ftp密码
 46     private String ftpPassword = "CHN_yzf486%#!";
 47     //Ftp的端口号,默认21
 48     private int ftpPort = 21;
 49     //Ftp服务器的文件路径
 50     private String ftpPath = "/home/weblogic/ftp/yzf/";
 51     //上传文件名称
 52     //private String fileName = "Teacher.pdf";*/
 53 
 54     private File storeFile;
 55     private SqlMapExe sqlexe;
 56     Connection conn=null;
 57     //更新RecruitActivity表
 58     private static final String UPDATE_RECRUIT_ACTIVITY="UPDATE RECRUITACTIVITY "
 59             + "SET FILEINPUT=? WHERE LOGINPHONE=?";
 60     private String  userNumber;
 61 
 62     // 上传文件存储目录
 63     private static final String UPLOAD_DIRECTORY = "upload";
 64 
 65     // 上传配置
 66     private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
 67     private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
 68     private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB
 69 
 70     @Override
 71     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 72 
 73         //获取用户的号码,根据用户的号码更新表信息
 74         userNumber = req.getParameter("userNumber");
 75         System.out.println("获取用户的userNumber为:"+userNumber);
 76 
 77         //检测是否为多媒体上传
 78         if (!ServletFileUpload.isMultipartContent(req)) {
 79             // 如果不是则停止
 80             PrintWriter writer = resp.getWriter();
 81             writer.println("Error: 表单必须包含 enctype=multipart/form-data");
 82             writer.flush();
 83             return;
 84         }
 85 
 86         // 配置上传参数
 87         DiskFileItemFactory factory = new DiskFileItemFactory();
 88         // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
 89         factory.setSizeThreshold(MEMORY_THRESHOLD);
 90         // 设置临时存储目录
 91         factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
 92         ServletFileUpload upload = new ServletFileUpload(factory);
 93 
 94         // 设置最大文件上传值
 95         upload.setSizeMax(MAX_FILE_SIZE);
 96         // 设置最大请求值 (包含文件和表单数据)
 97         upload.setSizeMax(MAX_REQUEST_SIZE);
 98         // 中文处理
 99         upload.setHeaderEncoding("UTF-8");
100 
101         // 构造临时路径来存储上传的文件
102         // 这个路径相对当前应用的目录
103         String uploadPath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIRECTORY;
104         //String uploadPath = getServletContext().getRealPath("/")  + UPLOAD_DIRECTORY;
105         //String uploadPath = "/home/weblogic/Oracle/Middleware/user_projects/domains/APPdomain/upload/CtWeb/resume_pdf/";
106         //String uploadPath = "/home/weblogic/ftp/yzf";
107 
108         // 如果目录不存在则创建
109         File uploadDir = new File(uploadPath);
110         if (!uploadDir.exists()) {
111             uploadDir.mkdir();
112         }
113 
114         try {
115             // 解析请求的内容提取文件数据
116             @SuppressWarnings("unchecked")
117             List<FileItem> formItems = upload.parseRequest(req);
118 
119             if (formItems != null && formItems.size() > 0) {
120                 // 迭代表单数据
121                 for (FileItem item : formItems) {
122                     // 处理不在表单中的字段
123                     if (!item.isFormField()) {
124                         String fileName = new File(item.getName()).getName();
125                         System.out.println("获取用户上传的文件名为:"+fileName);
126                         /*upLoadFilePdf(uploadPath,fileName);*/
127                         //String filePath=uploadPath+File.separator+fileName;
128                         String filePath = "/home/weblogic/Oracle/Middleware/user_projects/domains/newwap/upload/wap/upload/" + fileName;
129                         //String filePath = uploadPath + fileName;
130                         storeFile = new File(filePath);
131                         // 在控制台输出文件的上传路径
132                         System.out.println(filePath);
133                         // 保存文件到硬盘
134                         item.write(storeFile);
135                         req.setAttribute("message", "文件上传成功!");
136                         //System.out.println("文件上传成功!");
137                         //upLoadFilePdf(uploadPath,fileName);
138                         upLoadFilePdf(fileName);
139                         //删除本地临时文件
140                         //storeFile.delete();
141                     }
142                 }
143             }
144         } catch (Exception ex) {
145             req.setAttribute("message", "错误信息: " + ex.getMessage());
146         }
147     }
148 
149     /**
150      * location:134.224.165.193
151      * username:wgzx
152      * password:5Z9k%ppNrsuTFlh3
153      * path:/home/weblogic/Oracle/Middleware/user_projects/domains/APPdomain/upload/CtWeb/resume_pdf/
154      * @param
155      * @param file_name
156      */
157     //public void upLoadFilePdf(String file_path, String file_name) {
158     public void upLoadFilePdf(String file_name) {
159         try {
160 
161             SqlExe sqlExe=new SqlExe();
162             boolean b = sqlExe.execUpdateForList(UPDATE_RECRUIT_ACTIVITY, new String[]{file_name, userNumber});
163             System.out.println("retBoolean:"+b);
164 
165             String tmpPath = System.getProperty("java.io.tmpdir");
166             //FileInputStream in=new FileInputStream(new File(file_path + file_name));
167             FileInputStream in=new FileInputStream(storeFile);
168             FtpUtilActivity.uploadFile(ftpHost, ftpUserName,  ftpPassword, ftpPort, ftpPath, file_name, in);
169             //简历上传成功,同时更新简历表,更新文件上传字段
170             /*SqlExe sqlExe=new SqlExe();
171             boolean b = sqlExe.execUpdateForList(UPDATE_RECRUIT_ACTIVITY, new String[]{file_name, userNumber});
172             System.out.println("retBoolean:"+b);*/
173         }catch (Exception e){
174             e.printStackTrace();
175             System.out.println("Exception:"+e.getMessage());
176         }
177     }
178 
179     @Override
180     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
181         this.doPost(req,resp);
182     }
183 }

 

posted @ 2021-04-15 15:16  辰熙博客  阅读(556)  评论(0)    收藏  举报