commons-fileupload.jar包是Java设计中必备的jar包,主要用来实现maven中文件上传时控制大小的组件,封装了对流操作的全过程,简化了文件上传的代码复杂度,只需合理运用类中的方法就可以达到文件上传的效果。需要的编程人员可以下载!

Commons-fileupload组件实现文件下载的方法
// 文件的下载
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 找到用户所选定的文件
String uuidname = request.getParameter(“filename”);
uuidname = new String(uuidname.getBytes(“ISO8859-1”), “utf-8”);
String realname = uuidname.substring(uuidname.indexOf(“_”)+1);
// 确定文件的保存位置
String savePath = getFileAddr(realname);
File f = new File(savePath + “\\” + uuidname);
//System.out.println(savePath+“
”+uuidname);
if (f.exists()) {
// 设置应答的相应消息头
response.setContentType(“application/x-msdownload”);
String str = “attachment;filename=”+ java.NET.URLEncoder.encode(realname, “utf-8”);
response.setHeader(“Content-Disposition”, str);
// 创建一 个输入流对象和指定的文件相关联
FileInputStream in = new FileInputStream(f);
// 从response对象中获取到输出流对象
OutputStream out = response.getOutputStream();
// 从输入流对象中读数据写入到输出流对象中
byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
}else{
request.setAttribute(“message”, “下载资源不存在”);
request.getRequestDispatcher(“/message.jsp”)。forward(request, response);
}
}
//根据文件名查找路径
private String getFileAddr(String filename) {
int dir1 = filename.hashCode() & 0x0f;
int dir2 = filename.hashCode()》4 & 0x0f;
System.out.println(dir1+“====”+dir2);
String savePath = this.getServletContext()。getRealPath(“WEB-INF/upload”)
+ “\\” + dir1 + “\\” + dir2;
System.out.println(“=============”+savePath);
return savePath;
}