一.写一个jsp下载的界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h2>文件上传</h2>
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
用户名<input type="text" name="username"></form></br>
上传的文件一<input type="file" name="file1"></form></br>
上传的文件二<input type="file" name="file2"></form></br>
<input type="submit" value="提交"></form></br>
</form>
</body>
</html>
二.在src创建一个包web写上uploadservlet
@WebServlet(name = "UploadServlet",value = "/upload")
@MultipartConfig(maxFileSize = 1024*1024*5,maxRequestSize = 1024*1024*50)
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收文件
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Collection<Part> parts=request.getParts();
String basepath=request.getServletContext().getRealPath("/WEB-INF/upload");
File dir=new File(basepath);
System.out.println(dir.getAbsoluteFile());
if (!dir.exists()){
System.out.println("创建了文件夹");
dir.mkdirs();
}
//2.遍历集合
PrintWriter out=response.getWriter();
if (parts!=null){
for (Part part : parts) {
if (part.getSubmittedFileName()==null){
System.out.println(part.getName()+":"+request.getParameter(part.getName()));
}else {
String submittedFileName = part.getSubmittedFileName();
if (submittedFileName==""){
continue;
}
//兼容的操作
String header=part.getHeader("content-disposition");
String path = header.substring(header.indexOf("filename=") + 10, header.length() - 1);
String filename = path.substring(path.lastIndexOf("\\") + 1);
//获取文件的后缀
String ext=filename.substring(filename.lastIndexOf(".")+1);
List<String> allows= Arrays.asList("jpg","png","bmp");
if (!allows.contains(ext)){
out.write("文件类型不支持"+filename);
return;
}
//string filename=part.getsubmittenfilename();
part.write(UploadUtils.creatNewPath(basepath, filename)+"/"+ UploadUtils.createNewFilename(filename));
part.delete();
System.out.println("上传成功"+filename);
out.write("上传成功"+filename);
}
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
三.创建utils包写一个uploadutils(完成上传,登录jsp界面,上传可以在编译器上展示出来)
import java.io.File;
import java.util.HashMap;
import java.util.UUID;
public class UploadUtils {
public static void main(String[] args) {
createNewFilename("xxx");
}
public static String createNewFilename(String filename){
String s= UUID.randomUUID().toString().replace("-", "");
System.out.println(s);
return s+"-"+filename;
}
public static String creatNewPath(String basepath,String filename){
int hashCode=filename.hashCode();
int dir1=hashCode&0xf;
int dir2=(hashCode&0xf0)>>4;
String newpath=basepath+"/"+dir1+"/"+dir2;
File dir=new File(newpath);
if (!dir.exists()){
dir.mkdirs();
}
return newpath;
}
public static void listFiles(File dir, HashMap<String,String> map){
File[] files=dir.listFiles();
if (files!=null){
for (File file : files) {
if (file.isDirectory()){
listFiles(dir, map);
}else {
map.put(file.getName(),file.getName().split("_")[1]);
}
}
}
}
}
上传成功