下载路径:https://pan.baidu.com/s/1yKTH-rDfEBAHk7zFI9T_Mw
相关代码:
前端代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
${error}
异常机制:
package download_upload.download;
public class wenjianexception extends RuntimeException{
public wenjianexception() {
super();
// TODO Auto-generated constructor stub
}
public wenjianexception(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public wenjianexception(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
user类
package download_upload.download;
import lombok.Data;
@Data
public class user {
private String name;
private int age;
private String img;
}
utils工具类
package download_upload.download;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.naming.SizeLimitExceededException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
public class utils {
public static void wenjian(HttpServletRequest req,Map
{
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
// Create a factory for disk-based file items//创建基于磁盘的文件项的工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024*500);
factory.setRepository(new File("D:/"));
// Create a new file upload handler
//创建新文件上传处理程序
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(500*1024);
upload.setSizeMax(800*1024);
// Parse the request
//解析请求
try {
List
for (FileItem item : items) {
if (item.isFormField()) {
// String fieldName = item.getFieldName();
// long sizeInBytes = item.getSize();
//String wenjian=item.getString();
//System.out.println(fieldName+","+sizeInBytes+","+wenjian);
//System.out.println("-------------------------------");
map.put(item.getFieldName(), item.getString().toString());
}
else {
//String fieldName = item.getFieldName();
//System.out.println(fieldName);
// long sizeInBytes = item.getSize();
//String wenjian=item.getString("UTF-8");
//System.out.println(fieldName+","+sizeInBytes+","+wenjian);
System.out.println("-------------------------------");
try {
if(!item.getContentType().startsWith("image/"))
{
throw new wenjianexception("亲!您上传的文件格式有误,请重新上传!!!!");
}
else {
String filename=UUID.randomUUID().toString()+"."+FilenameUtils.getExtension(item.getName());
item.write(new File(new File(req.getServletContext().getRealPath("images")),filename));
map.put(item.getFieldName(), filename);
}
}catch(wenjianexception e) {
throw e;
}
catch(FileSizeLimitExceededException e) {
throw new wenjianexception("亲,你的文件超过了500kb,请重新上传");
}catch(SizeLimitExceededException e) {
throw new wenjianexception("亲,您的总文件请求超过800kb,请重新上传");
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
servlet类
package download_upload.download;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/down")
public class download extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
try {
user u=new user();
Map
utils.wenjian(req,map);
u.setName(map.get("name").toString());
u.setAge(Integer.valueOf( map.get("age").toString()));
u.setImg(map.get("images").toString());
System.out.println(u);
req.setAttribute("error","注册成功");
req.getRequestDispatcher("/download.jsp").forward(req, resp);
} catch (wenjianexception e) {
// TODO: handle exception
req.setAttribute("error",e.getMessage());
req.getRequestDispatcher("/download.jsp").forward(req, resp);
}
}
}
使用uuid后的显示结果
下载功能的实现
前端代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
后台代码:
package download_upload.upload;
import java.io.File;
import java.io.IOException;
importjava.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/upload")
public class upload extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("application/x-msdownload");
req.setCharacterEncoding("UTF-8");
String name=req.getHeader("User-Agent");
String filename=req.getParameter("filename");
if(name.contains("MSIE"))
{
resp.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
}else {
resp.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("UTF-8"),"ISO-8859-1"));
}
System.out.println(filename);
String path=req.getServletContext().getRealPath("/WEB-INF/download");
File file=new File(path, filename);
ServletOutputStream out=resp.getOutputStream();
Files.copy(Paths.get(file.getAbsolutePath()),out);
}
}
整体结构图解: