jsp界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>smartUploadServlet</title>
</head>
<body>
<h2> 文件批量上传</h2>
<form action="smartUploadServlet" 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>
<a href="SmartDown?filename=4.jpg">4.jpg</a>
</body>
</html>
smartUploadServlet实现:
package Servlet;
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;
/**
* Servlet implementation class smartUploadServlet
*/
public class smartUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
String filepathString = getServletContext().getRealPath("/") + "images";
File file = new File(filepathString);
if (!file.exists()) {
file.mkdir();
}
SmartUpload sUpload = new SmartUpload();
sUpload.initialize(getServletConfig(), request, response);
sUpload.setMaxFileSize(1024 * 1024 * 10);
sUpload.setTotalMaxFileSize(1024 * 1024 * 100);
sUpload.setAllowedFilesList("txt,jpg");
String resultString = "上传成功!";
try {
sUpload.setDeniedFilesList("rar,jsp");
sUpload.upload();
int count = sUpload.save(filepathString);
request.setAttribute("result", resultString);
System.out.println("ok:" + count);
} catch (Exception e) {
// TODO: handle exception
resultString = "上传失败!";
if (e.getMessage().indexOf("1015") != -1) {
resultString = "文件类型不正确!";
}
e.printStackTrace();
}
for (int i = 0; i < sUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File tempfile = sUpload.getFiles().getFile(i);
System.out.println("name:"+ tempfile.getFieldName());
System.out.println("fileext:"+ tempfile.getFileExt());
System.out.println("size:"+ tempfile.getSize());
System.out.println("pathname:"+ tempfile.getFilePathName());
}
System.out.println(filepathString);
request.getRequestDispatcher("01.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
SmartDown.java:
package Servlet;
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;
/**
* Servlet implementation class SmartDown
*/
public class SmartDown extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
String filenaemString= request.getParameter("filename");
SmartUpload s = new SmartUpload();
s.initialize(getServletConfig(), request, response);
s.setContentDisposition(null);
try {
s.downloadFile("/images/"+filenaemString);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
需要jar包
未完待续。。。。。。