Java实现上传下载文件
一、上传源码
private void upload(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(10*1024*1024);
upload.setSizeMax(50*1024*1024);
upload.setHeaderEncoding("UTF-8");
if (upload.isMultipartContent(request)) {
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list){
if (item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
System.out.println(value);
}
else {
String name = item.getName();
String id = UUID.randomUUID().toString();
name = id + "#" + name;
String basePath = getServletContext().getRealPath("/upload");
File file = new File(basePath,name);
item.write(file);
item.delete();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
二、下载源码
private void down(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
String fileName = request.getParameter("fileName");
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
String basePath = getServletContext().getRealPath("/upload");
InputStream in = new FileInputStream(new File(basePath,fileName));
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = in.read(b)) != -1){
out.write(b, 0, len);
}
out.close();
in.close();
}
三、测试案例
package cn.demo.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if ("upload".equals(method)) {
upload(request,response);
}else if ("downList".equals(method)) {
downList(request,response);
}else if ("down".equals(method)) {
down(request,response);
}
}
private void upload(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(10*1024*1024);
upload.setSizeMax(50*1024*1024);
upload.setHeaderEncoding("UTF-8");
if (upload.isMultipartContent(request)) {
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list){
if (item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
System.out.println(value);
}
else {
String name = item.getName();
String id = UUID.randomUUID().toString();
name = id + "#" + name;
String basePath = getServletContext().getRealPath("/upload");
File file = new File(basePath,name);
item.write(file);
item.delete();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void downList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
Map<String,String> fileNames = new HashMap<String,String>();
String bathPath = getServletContext().getRealPath("/upload");
File file = new File(bathPath);
String list[] = file.list();
if (list != null && list.length > 0){
for (int i=0; i<list.length; i++){
String fileName = list[i];
String shortName = fileName.substring(fileName.lastIndexOf("#")+1);
fileNames.put(fileName, shortName);
}
}
request.setAttribute("fileNames", fileNames);
request.getRequestDispatcher("/downlist.jsp").forward(request, response);
}
private void down(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = request.getParameter("fileName");
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
String basePath = getServletContext().getRealPath("/upload");
InputStream in = new FileInputStream(new File(basePath,fileName));
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = in.read(b)) != -1){
out.write(b, 0, len);
}
out.close();
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
注意:有用记得点收藏哟!