欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Smartupload实现文件批量上传与下载

程序员文章站 2022-03-15 09:21:39
...

以下内容为smartupload实现的文件上传与下载,详细的配置文件如何写,请移步https://blog.csdn.net/qq_38052920/article/details/89451026
1、首先下载一个jar包,下载地址https://download.csdn.net/download/WOkeni/2614115
Smartupload实现文件批量上传与下载
2、在jsp页面中写入

<form action="SmartuploadServlet.do" method="post" enctype="multipart/form-data">
     上传文件1:<input type="file" name="myfile1"><br>
     上传文件2:<input type="file" name="myfile2"><br>
     上传文件3:<input type="file" name="myfile3"><br>
     <input type="submit" value="提交">${result}
  </form>

3、web.xml写入

<servlet>
    <servlet-name>SmartuploadServlet</servlet-name>
    <servlet-class>servlet.SmartuploadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SmartuploadServlet</servlet-name>
    <url-pattern>/SmartuploadServlet.do</url-pattern>
  </servlet-mapping>

4、servlet文件写入

package servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

/**
 * Servlet implementation class SmartuploadServlet
 */
@WebServlet("/SmartuploadServlet")
public class SmartuploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SmartuploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置上传文件保存路径
		String filePath = getServletContext().getRealPath("/") + "file";
		File file = new File(filePath);
		if(!file.exists()) {
			file.mkdir();
		}
		
		SmartUpload su = new SmartUpload();
		//初始化对象
		su.initialize(getServletConfig(), request, response);
		//设置上传文件的大小
		su.setMaxFileSize(1024*1024*10);
		//设置所有文件的大小
		su.setTotalMaxFileSize(1024*1024*100);
		//设置允许上传文件的类型
		su.setAllowedFilesList("txt,jpg,png,rar,zip,mp4,avi,doc,docx");
		String result = "上传成功";
		//设置禁止上传文件的类型
		try {
			su.setDeniedFilesList("gif");
			//上传文件
			su.upload();
			
			int count = su.save(filePath);
			System.out.println("上传成功" + count + "个文件!");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result = "上传失败!";
			e.printStackTrace();
		}
		request.setAttribute("result", result);
		request.getRequestDispatcher("./upLoadFile.jsp").forward(request, response);
		
	}

}

5、启动项目进行测试
Smartupload实现文件批量上传与下载
我在桌面创建了一个txt文件,一个doc文件,一个png文件
Smartupload实现文件批量上传与下载
Smartupload实现文件批量上传与下载
6、文件下载
配置web.xml不再赘述
jsp页面

Smartupload实现文件批量上传与下载
SmartdownloadServlet.java

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class SmartdownloadServlet
 */
@WebServlet("/SmartdownloadServlet")
public class SmartdownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SmartdownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String filename = request.getParameter("filename");
		
		SmartUpload su = new SmartUpload();
		su.initialize(getServletConfig(), request, response);
		su.setContentDisposition(null);
		try {
			su.downloadFile("/file/" + filename);
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

测试一下
Smartupload实现文件批量上传与下载
7、批量文件下载
jsp页面

<form action="BatchSmartdownloadServlet.do">
    <input type="checkbox" name="filename" value="ok.txt">文件1
    <input type="checkbox" name="filename" value="test.doc">文件2
    <input type="submit" value="下载">
  </form>

servlet文件

package servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BatchSmartdownloadServlet
 */
@WebServlet("/BatchSmartdownloadServlet")
public class BatchSmartdownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BatchSmartdownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/x-msdownload");
		response.setHeader("Content-Disposition", "attachment;filename=test.zip");
		
		String path = getServletContext().getRealPath("/") + "file/";
		String[] filenames = request.getParameterValues("filename");
		String str = "";
		String rt = "\r\n";
		ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
		for(String filename : filenames) {
			str += filename + rt;
			File file = new File(path + filename);
			zos.putNextEntry(new ZipEntry(filename));
			FileInputStream fis = new FileInputStream(file);
			byte b[] = new byte[1024];
			int n = 0;
			while((n = fis.read(b)) != -1) {
				zos.write(b, 0, n);
			}
			zos.flush();
			fis.close();
		}
		zos.setComment("download success:" + rt + str);
		zos.flush();
		zos.close();
	}

}

写完测试一下
Smartupload实现文件批量上传与下载