1.index.jsp
<body>
<form action="SmartUploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file"/><br>
<input type="file" name="file"/><br>
<input type="file" name="file"/><br>
<input type="file" name="file"/><br>
<input type="file" name="file"/><br>
文件描述<br><textarea rows="3" cols="20" name="desc"></textarea><br>
<input type="submit" value="上传"/>
</form>
</body>
2.SmartUploadServlet
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class SmartUploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//存储所有文件名
List<String> fileNames = new ArrayList<String>();
//获取服务器文件存储的根路径
String root = this.getInitParameter("root");
try {
//创建一个smartUpload对象
SmartUpload su = new SmartUpload();
//初始化配置
su.initialize(this, request, response);
//设置允许上传的文件列表
//su.setAllowedFilesList("jpg,png,gif,bmp");
//设置不允许上传的文件列表
//su.setDeniedFilesList(".exe,.bat,.dll")
//设置单个上传文件的最大大小(20M,默认为2M)
su.setMaxFileSize(1024*1024*20);
//设置总共文件允许的大小
su.setTotalMaxFileSize(1024*1024*1024*2);
//开始上传
su.upload();
//获取上传文件的列表
Files files = su.getFiles();
for(int i = 0;i<files.getCount();i++){
//获取指定文件
com.jspsmart.upload.File file = files.getFile(i);
//将文件名加入集合
fileNames.add(file.getFileName());
System.out.println("要上传的文件:"+file.getFileName());
//将客户端的文件复制到服务器的空文件中
file.saveAs(root+File.separator+file.getFileName());
System.out.println(root+File.separator+file.getFileName());
}
System.out.println("文件上传成功!");
//获取request对象(SmartUpload)
Request req = su.getRequest();
//根据控件名取得控件值
String desc = req.getParameter("desc");
System.out.println(desc);
request.setAttribute("files", fileNames);
request.getRequestDispatcher("showFile.jsp").forward(request, response);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.showFile.jsp
<body>
<table width="400px" border="1">
<caption>您上传的文件列表</caption>
<tr>
<th>文件名</th>
<th>操作</th>
</tr>
<c:forEach items="${files}" var="f">
<tr>
<td>${f}</td>
<td><a href="FileDownLoadServlet?fname=${f}">下载</a></td>
</tr>
</c:forEach>
</table>
</body>
4.FileDownLoadServlet
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownLoadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("fname");
System.out.println(name+"!!!");
String root = this.getInitParameter("root");
File f = new File(root,name);
//设置响应的方式(以附件响应)
response.setHeader("Content-Disposition", "attachment;fileName="+URLEncoder.encode(name, "utf-8"));
response.setContentLength((int)f.length());//设置下载文件大小
ServletOutputStream sos = response.getOutputStream();
PrintStream ps = new PrintStream(sos);
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024*1024];
int len = 0;
while((len = bis.read(b)) != -1){
ps.write(b, 0, len);
ps.flush();
}
}
}
5.web.xml
<servlet>
<servlet-name>FileDownLoadServlet</servlet-name>
<servlet-class>com.servlet.FileDownLoadServlet</servlet-class>
<init-param>
<param-name>root</param-name>
<param-value>G:\\fileupload</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>SmartUploadServlet</servlet-name>
<servlet-class>com.servlet.SmartUploadServlet</servlet-class>
<init-param>
<param-name>root</param-name>
<param-value>G:\\fileupload</param-value>
</init-param>
</servlet>
把这个jar包粘贴到lib下面
思考:
文件上传成功,可是报错不能跳转到showFile.jsp
报错信息:com.jspsmart.upload.SmartUploadException: File can't be saved (1120).