Servlet页面
package com.demo;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/fileUpload.do")
@MultipartConfig(location="D:\\",fileSizeThreshold=1024)
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String getFilename(Part part)
{
String fname=null;
String header=part.getHeader("content-disposition");
fname=header.substring(header.lastIndexOf("=")+2,header.length()-1);
fname=fname.substring(fname.lastIndexOf("\\")+1);
fname=UUID.randomUUID()+"_"+fname;
return fname;
}
private String getFilename1(Part part)
{
String fname=null;
String header=part.getHeader("content-disposition");
fname=header.substring(header.lastIndexOf("=")+2,header.length()-1);
fname=fname.substring(fname.lastIndexOf("\\")+1);
return fname;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String path =this.getServletContext().getRealPath("/");
String mnumber=request.getParameter("mnumber");
Part p=request.getPart("fileName");
Part p1=request.getPart("fileName1");
Part p2=request.getPart("fileName2");
String message="";
if(p.getSize()>1024*1024||p1.getSize()>1024*1024||p2.getSize()>1024*1024)
{
p.delete();
p1.delete();
p2.delete();
message="文件太大,不能上传";
}else
{
path=path+"\\member\\"+mnumber;
File f=new File(path);
if(!f.exists()) {
f.mkdirs();
}
PrintWriter out =response.getWriter();
String fname=getFilename(p);
String fname1=getFilename(p1);
String fname2=getFilename(p2);
p1.write(path+"\\"+fname1);
p.write(path+"\\"+fname);
p2.write(path+"\\"+fname2);
message="文件上传成功!";
String fnamex2=getFilename1(p);
String fnamex1=getFilename1(p);
String fnamex=getFilename1(p);
out.print(fnamex+message+"<br>");
out.print(fnamex1+message+"<br>");
out.print(fnamex2+message+"<br>");
}
}
}
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传文件</title>
</head>
<body>
${message }<br>
<form action="fileUpload.do" enctype="multipart/form-data" method="post">
<table>
<tr><td colspan="2" align="center">上传文件</td></tr>
<tr>
<td>文件名称1:</td>
<td ><input type="file" name="fileName" size="30"></td>
</tr>
<tr>
<td>文件名称2:</td>
<td ><input type="file" name="fileName1" size="30"></td>
</tr>
<tr>
<td>文件名称3:</td>
<td ><input type="file" name="fileName2" size="30"></td>
</tr>
<tr>
<td align="right"><input type="submit" value="提交"></td>
<td align="left"><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>