本项目需要smartupload.jar
- 项目结构
jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="smartupload" 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="myfile2"><br>
<input type="submit" value="提交">${result}
</form>
<hr>
单文件下载 <a href="downloadservlet?filename=3.jpg">3.jpg</a>
<hr>
<h2>文件批量下载</h2>
<form action="batchdownservlet">
<input type="checkbox" name="filename" value="1.jpg" > 1.jpg
<input type="checkbox" name="filename" value="2.jpg" > 2.jpg
<input type="checkbox" name="filename" value="3.jpg" > 3.jpg
<input type="submit" value="下载">
</form>
</body>
</html>
文件上传代码
package test;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
public class smartupload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public smartupload() {
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
System.out.println("接受到请求");
System.out.println(request.getParameter("charsetName"));
String filepath="D:images";
File file=new File(filepath);
if (!file.exists()) {
file.mkdirs();
}
SmartUpload su=new SmartUpload();
su.initialize(getServletConfig(), request, response);
su.setMaxFileSize(1024*1024*10);
su.setTotalMaxFileSize(1024*1024*10);
su.setAllowedFilesList("txt,jpg,gif");
su.setCharset("utf-8");
String result="上传成功";
try {
su.setDeniedFilesList("rar,jsp,gif");
su.upload();
int count=su.save(filepath);
System.out.println("上传成功"+count);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
result="上传失败";
if (e.getMessage().indexOf("1015")!=-1) {
result="上传文件失败:上传文件 类型不正确";
}else if (e.getMessage().indexOf("1010")!=-1) {
result="上传文件失败:上传文件类型不正确";
}else if (e.getMessage().indexOf("1105")!=-1) {
result="上传文件失败,上传文件大小大于允许上传的最大值";
}else if (e.getMessage().indexOf("1110")!=-1) {
result="上传文件失败,上传文件总大小大于允许上传总大小的";
}
}
for(int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File tempFile=su.getFiles().getFile(i);
System.out.println("获取上传文件的信息");
System.out.println("文件名"+tempFile.getFieldName());
System.out.println("文件大小"+tempFile.getSize());
System.out.println("文件扩展名"+tempFile.getFileExt());
}
request.setAttribute("result", result);
request.getRequestDispatcher("2.jsp").forward(request, response);
}
}
单文件下载
package test;
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 DownloadServlet
*/
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DownloadServlet() {
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
System.out.println("得到响应");
String filename=request.getParameter("filename");
SmartUpload su=new SmartUpload();
su.initialize(getServletConfig(), request, response);
su.setContentDisposition(null);
try {
su.downloadFile("D:/images/"+filename);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
多文件下载
package test;
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class BatchDownServlet
*/
public class BatchDownServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BatchDownServlet() {
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
System.out.println("得到响应");
response.setContentType("application/x-msdownload");
//设置文件以压缩包形式下载
response.setHeader("Content-Disposition", "attchment;filename=test.zip");
String path="D:/images/";
String[] filenames=request.getParameterValues("filename");
ZipOutputStream zos=new ZipOutputStream(response.getOutputStream());
//把文件内容写到压缩包中
for(String filename:filenames)
{
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.flush();
zos.close();
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>smartupload</servlet-name>
<servlet-class>test.smartupload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>smartupload</servlet-name>
<url-pattern>/smartupload</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>downloadservlet</servlet-name>
<servlet-class>test.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>downloadservlet</servlet-name>
<url-pattern>/downloadservlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>batchdownservlet</servlet-name>
<servlet-class>test.BatchDownServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>batchdownservlet</servlet-name>
<url-pattern>/batchdownservlet</url-pattern>
</servlet-mapping>
</web-app>