Java实现文件的上传下载

文件上传,下载的方法:

上传代码

 1 /**
 2      * 文件上传、保存
 3      * 
 4      * @param mapping
 5      * @param form
 6      * @param request
 7      * @param response
 8      * @return
 9      * @throws IOException
10      */
11     @SuppressWarnings("unchecked")
12     public ActionForward tzupload(ActionMapping mapping, ActionForm form,
13             HttpServletRequest request, HttpServletResponse response)
14             throws Exception {
15         BaseActionForm cForm = (BaseActionForm) form;
16         Dto reDto = cForm.getParamAsDto(request);
17         String uuid = new UUIDGenerator().create();
18         String ggid = new UUIDGenerator().create();
19         UserInfoVo userInfo = WebUtils.getSessionContainer(request).getUserInfo();
20         String gglx = "ggxx";
21         reDto.put("uuid", uuid);
22         reDto.put("ggid", ggid);
23         reDto.put("yxbz", "Y");
24         reDto.put("lrr_dm", userInfo.getUserid());
25         
26         FormFile formfile = cForm.getFile1();
27         String filename = formfile.getFileName();// 获取文件的名字
28         // 获取当前时间
29         Date date = new Date();
30         SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
31         String dqsj = simpledate.format(date);
32         String urlfile = readProperties.getProperties().getProperty(
33                 "FILEUPLOAD1")
34                 + formfile;
35         final String wjlj = readProperties.getProperties().getProperty(
36                 "FILEUPLOAD2")
37                 + formfile;
38         
39         if (filename.length() > 0) {
40             reDto.put("wjlj", wjlj);
41             reDto.put("wjmc", filename);
42             reDto.put("ggid", ggid);
43             reDto.put("gglx", gglx); 
44             reDto.put("scsj", dqsj);
45             reDto.put("lrrq", dqsj);
46             reDto.put("yxbz", "Y");
47             reDto.put("lrr_dm", reDto.getAsString("lrr_dm"));
48             mobilesBsService.updatewjlj(reDto);
49 
50             File file = new File(urlfile);
51             if (file.exists()) {
52                 file.mkdir();// 创建路径
53             }
54             File filecreate = new File(urlfile);
55             // 检查路径是否
56             if (!filecreate.exists()) {
57                 FileOutputStream out = new FileOutputStream(filecreate);
58                 out.write(formfile.getFileData());
59                 out.flush();
60                 out.close();
61             } else {
62                 FileOutputStream out = new FileOutputStream(filecreate);
63                 out.write(formfile.getFileData());
64                 out.flush();
65                 out.close();
66             }
67 
68         }
69         Dto pDto = mobilesBsService.saveTzggDatas(reDto);
70         pDto.put("gglx", gglx);
71         mobilesBsService.createHtml(pDto);
72         String wjname = ggid + ".html";
73         pDto.put("wjname", wjname);
74         mobilesBsService.saveHtml(pDto);
75         
76         setOkTipMsg("文件上传成功", response);
77         return mapping.findForward(null);
78 
79     }
80     

下载代码

 1 /**
 2      * 文件下载
 3      * 
 4      * @param mapping
 5      * @param form
 6      * @param request
 7      * @param reponse
 8      * @return
 9      * @throws Exception 
10      */
11     public ActionForward downloadfile(ActionMapping mapping, ActionForm form,
12             HttpServletRequest request, HttpServletResponse response)
13             throws Exception, Exception {
14         // private final static String
15         String filename = request.getParameter("wjmc");
16         filename = new String(filename.getBytes("ISO-8859-1"), "utf-8");
17         String filesavepath = request.getRealPath("/upload/fileupload1");
18         //String path = findFileSavePathByFileName(filename,filesavepath);
19         //得到要下载的文件
20         File file = new File(filesavepath+"\\"+filename);
21         if(!file.exists()){
22             request.setAttribute("message", "要下载的文件已删除");
23             request.getRequestDispatcher("/message.jsp").forward(request, response);
24         }
25         //处理文件名
26         String realname = filename.substring(filename.indexOf("_")+1);
27         response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
28         //创建文件输入流
29         FileInputStream in = new FileInputStream(file);
30         //创建输出流
31         OutputStream out = response.getOutputStream();
32         //创建缓存区
33         byte buffer[] = new byte[1024];
34         int len = 0;
35         while((len = in.read(buffer))>0){
36             //输出缓冲区的内容到浏览器,实现文件下载
37         out.write(buffer, 0, len);
38         }
39         //关闭文件输入流
40         in.close();
41         //关闭输出流
42         out.close();
43         return mapping.findForward(null);
44     }
 1     /**
 2      * 通过文件名和上传文件根目录找出要下载的文件
 3      * @param filename
 4      * @param saveRootPath
 5      * @return
 6      */
 7     public String findFileSavePathByFileName(String filename,
 8             String saveRootPath) {
 9         int hashcode = filename.hashCode();
10         int dir1 = hashcode & 0xf; // 0--15
11         int dir2 = (hashcode & 0xf0) >> 4; // 0-15
12         String dir = saveRootPath + "\\" + dir1 + "\\" + dir2; // upload\2\3
13                                                                 // upload\3\5
14         File file = new File(dir);
15         if (!file.exists()) {
16             // 创建目录
17             file.mkdirs();
18         }
19         return dir;
20     }

 

posted @ 2018-02-05 16:34  晒太阳的雪  阅读(2253)  评论(0编辑  收藏  举报