首页 > 代码库 > 上传代码分享commons-fileupload

上传代码分享commons-fileupload

public class AttachServlet extends HttpServlet{         /**     *      */    private static final long serialVersionUID = 14567895678L;        public static void main(String args[])    {                        Date date =new Date();        SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmm");//完整的时间        String time=sdf.format(date);        System.out.println(time);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             throws IOException, ServletException {              String resultStr = "";        String result = "";    String type  =  request.getParameter("type");    String name  =  request.getParameter("name");    String upbyname  =  request.getParameter("upbyname");    String upbyid  =  request.getParameter("upbyid");    String mark  =  request.getParameter("mark");        Date date =new Date();    SimpleDateFormat sdf1 =new SimpleDateFormat("yyyyMMddHHmmss");//完整的时间    SimpleDateFormat sdf2 =new SimpleDateFormat("yyyy-MM-dd HH:mm");//完整的时间    String time1=sdf1.format(date);    String time2=sdf2.format(date);    String uptime = time2;    AttachmentModel attachmentModel = new AttachmentModel();    attachmentModel.setType(type);    attachmentModel.setName(name);    attachmentModel.setUpbyid(upbyid);    attachmentModel.setUpbyname(upbyname);    attachmentModel.setMark(mark);    attachmentModel.setUptime(uptime);     try {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    //文件读写工厂                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       DiskFileItemFactory factory = new DiskFileItemFactory();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             // 文件分析工具。上传的file必须有name否则无法识别                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ServletFileUpload upload = new ServletFileUpload(factory);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             List<FileItem> items = upload.parseRequest(request);// 得到所有的文件              //遍历文件         Iterator<FileItem> i = items.iterator();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             while (i.hasNext()) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    FileItem fi = (FileItem) i.next();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   String upFileName = fi.getName();                             if (upFileName != null && !upFileName.equals("")) {                     File fullFile = new File(fi.getName());                 //realName 为当前的系统时间 + 文件名称。                  String finalName =  time1 + fullFile.getName();                 //获取附件上传的路径(security.properties文件中,一般的配置型的都放在里面)                 String uploadPath = PropertiesUtils.getProperty("attachment.path");                 File file = new File(uploadPath);                 if(!file.exists())                 {                     file.mkdirs();                 }                 File savedFile = new File(uploadPath, finalName);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           fi.write(savedFile);                                  attachmentModel.setRealname(finalName);                 AttachServiceImpl attachService = new AttachServiceImpl();                 resultStr = attachService.insertAttach(attachmentModel);             }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }                   response.setContentType("text/html;charset=utf-8");          String uri = request.getContextPath().toString() ;         //增加插入操作的返回值         if(resultStr.equals(""))         {             request.setCharacterEncoding("UTF-8");             result = "请正确的上传附件,您上传的附件为空!";             uri = uri  + "/creditease/attach/attachment/attachInsertFail.jsp?result="+java.net.URLEncoder.encode(result, "UTF-8");          }else         {             if(resultStr.equals("success"))             {                 result ="上传成功!!";                 uri = uri  + "/creditease/attach/attachment/attachInsertSuccess.jsp";             }else             {                 request.setCharacterEncoding("UTF-8");                 result = resultStr;                 uri = uri  + "/creditease/attach/attachment/attachInsertFail.jsp?result="+java.net.URLEncoder.encode(result, "UTF-8");              }         }         response.sendRedirect( uri);     } catch (Exception e) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  // 可以跳转出错页面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  e.printStackTrace();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   }               public void doGet(HttpServletRequest request, HttpServletResponse response)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            throws IOException, ServletException {           this.doPost(request, response);    }}

 

上传代码分享commons-fileupload