java文件上传下载 使用SmartUpload组件实现

 使用SmartUpload组件实现(下载jsmartcom_zh_CN.jar) 2017-11-07  

1、在WebRoot创建以下文件夹,css存放样式文件(css文件直接拷贝进去),images存放图片(图片也拷贝进去),js存放js文件(拷贝),jsp存放我们的jsp文件

java文件上传下载 使用SmartUpload组件实现_第1张图片

2、创建jsp文件 01.jsp

java文件上传下载 使用SmartUpload组件实现_第2张图片

3、编写jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8   
 9     
10     My JSP '01.jsp' starting page
11     
12     
13     
14     
15     
36   
37   
38   
39         

文件批量上传

40 41
42 43
44 45
46 47
48 49 ${result} 50
51
52 53

文件批量下载

54 55
56 Image2 57 Image3 58 Image4 59 60

       61
62 63

图片预览

64

Large Image

65

class="thumbs"> 66 67 68 69 70 71

72 73

4、后台创建SmartUploadServlet(上传)   web.xml配置路径,jsp文件中

do">必须和web.xml中一致

java文件上传下载 使用SmartUpload组件实现_第3张图片

 1 
 2  3 xmlns="http://java.sun.com/xml/ns/javaee" 
 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 5   scx
 6   
 7   
 8     SmartUploadServlet
 9     class>com.imooc.servlet.SmartUploadServletclass>
10   
11   
12   
13     SmartDownloadServlet
14     class>com.imooc.servlet.SmartDownloadServletclass>
15   
16   
17     BatchDownloadServlet
18     class>com.imooc.servlet.BatchDownloadServletclass>
19   
20 
21   
22     SmartUploadServlet
23     /smartUploadServlet.do
24   
25   
26   
27     SmartDownloadServlet
28     /smartDownloadServlet.do
29   
30   
31   
32     BatchDownloadServlet
33     /batchDownloadServlet.do
34   
35   
36   
37     index.jsp
38   
39 

 

5、编写SmartUploadServlet(多个上传,单个上传只需要把jsp文件中input留下一个)

 1 package com.imooc.servlet;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import com.jspsmart.upload.SmartUpload;
12 
13 public class SmartUploadServlet extends HttpServlet {
14 
15     /**
16      * 
17      */
18     private static final long serialVersionUID = 1L;
19 
20     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
21         doPost(req, resp);
22     }
23 
24     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
25 
26         req.setCharacterEncoding("UTF-8");
27         // 设置上传的保存路径
28         String filePath = getServletContext().getRealPath("/") + "images";
29         // 创建文件对象 如果存在就不创建,否则创建文件夹
30         File file = new File(filePath);
31         if (file.exists()) {
32             file.mkdir();
33         }
34         // 创建SmartUpload对象
35         SmartUpload su = new SmartUpload();
36         // 初始化对象
37         su.initialize(getServletConfig(), req, resp);
38         // 设置上传文件大小
39         su.setTotalMaxFileSize(1024 * 1024 * 100);
40         // 设置上传文件类型
41         su.setAllowedFilesList("txt,jpg,gif");
42         // 创建提示变量
43         String result = "上传成功";
44         try {
45             // 设置禁止上传类型
46             su.setDeniedFilesList("rar,jsp,js");
47             su.upload();
48             // 返回上传文件数量
49             int count = su.save(filePath);
50             System.out.println("上传成功" + count + "个文件!");
51 
52         } catch (Exception e) {
53             result = "上传失败";
54             e.printStackTrace();
55         }
56 
57         // 获取上传成功的文件的属性
58         for (int i = 0; i < su.getFiles().getCount(); i++) {
59             com.jspsmart.upload.File tempFile = su.getFiles().getFile(i);
60             System.out.println("---------------------");
61             System.out.println("表单当中name属性值:" + tempFile.getFieldName());
62             System.out.println("上传文件名:" + tempFile.getFieldName());
63             System.out.println("上传文件长度:" + tempFile.getSize());
64             System.out.println("上传文件的拓展名:" + tempFile.getFileExt());
65             System.out.println("上传文件的全名:" + tempFile.getFilePathName());
66             System.out.println("---------------------");
67         }
68         req.setAttribute("result", result);
69         req.getRequestDispatcher("jsp/01.jsp").forward(req, resp);
70     }
71 
72 }

6、SmartDownloadServlet(单个下载)
 1 package com.imooc.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import com.jspsmart.upload.SmartUpload;
11 import com.jspsmart.upload.SmartUploadException;
12 
13 public class SmartDownloadServlet extends HttpServlet {
14 
15     /**
16      * 
17      */
18     private static final long serialVersionUID = 1L;
19 
20     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21         doPost(request, response);
22     }
23 
24     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
25         // 获取文件名称
26         String filename = request.getParameter("filename");
27         SmartUpload su = new SmartUpload();
28         // 初始化
29         su.initialize(getServletConfig(), request, response);
30         // 把默认显示方式设为空
31         su.setContentDisposition(null);
32 
33         try {
34             su.downloadFile("/images/" + filename);
35         } catch (SmartUploadException e) {
36             e.printStackTrace();
37         }
38     }
39 }
7、BatchDownloadServlet(多个文件下载)
 1 package com.imooc.servlet;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import java.util.zip.ZipEntry;
 7 import java.util.zip.ZipOutputStream;
 8 
 9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 
14 public class BatchDownloadServlet extends HttpServlet {
15 
16     /**
17      * 
18      */
19     private static final long serialVersionUID = 1L;
20 
21     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22         doPost(req, resp);
23     }
24 
25     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26         resp.setContentType("application/x-msdownload");
27         // 以附件的形式下载
28         resp.setHeader("Content-Disposition", "attachment;filename=test.zip");
29 
30         // 获取下载路径
31         String path = getServletContext().getRealPath("/") + "images/";
32         // 获取文件数组
33         String[] filenames = req.getParameterValues("filename");
34         // 创建空字符串
35         String str = "";
36         // 换行符
37         String rt = "\r\n";
38         // 创建压缩包输出流
39         ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
40         // 遍历文件数组
41         for (String filename : filenames) {
42             str += filename + rt;
43             // 创建文件对象
44             File file = new File(path + filename);
45             zos.putNextEntry(new ZipEntry(filename));
46             // 创建文件输出流
47             FileInputStream fis = new FileInputStream(file);
48             byte[] b = new byte[1024];
49             int n = 0;
50             while ((n = fis.read(b)) != -1) {
51                 zos.write(b, 0, n);
52             }
53             zos.flush();
54             fis.close();
55         }
56         zos.setComment("成功" + rt + str);
57         zos.flush();
58         zos.close();
59     }
60 }

效果图
java文件上传下载 使用SmartUpload组件实现_第4张图片
 
   

 

 

 

 

你可能感兴趣的:(java文件上传下载 使用SmartUpload组件实现)