package webtest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet(“/UploadServlet”)
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID=1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);// 请求编码处理,传输时
response.setCharacterEncoding(“utf-8”);// 响应编码处理,传输时
response.setContentType(“utf-8”);// 响应编码处理,显示时
boolean isMultipart=ServletFileUpload.isMultipartContent(request);// 判断是否有entype=”multipart/form-data”部分
if (isMultipart) {
try {
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
//设置上传时的内存缓冲区大小,超出时写入临时文件 10kb
((DiskFileItemFactory) factory).setSizeThreshold(10240);//单位byte
//设置临时文件目录,防止系统空间不足
((DiskFileItemFactory) factory).setRepository(new File(request.getSession().getServletContext().getRealPath(“Uploadtemp”)));//动态获取Uploadtemp文件夹位于该项目中的位置
//控制上传单个文件的最大值 20kb
upload.setSizeMax(20480);//单位byte
List<FileItem> items=upload.parseRequest(request);// 解析form中的所有请求字段,并保存在item中
Iterator<FileItem> iter=items.iterator();
while (iter.hasNext()) {
// 解析时无顺序,因此需根据名字查找
FileItem item=iter.next();
if (item.isFormField()) {//非文件数据
String itemName=item.getFieldName();//获取非文件的范围数据名
if (itemName.equals(“sno”)) {
int sno=-1;
sno=Integer.parseInt(item.getString(“utf-8”));
}
else if (itemName.equals(“sname”)) {
String sname=null;
sname=item.getString(“utf-8”);
}
else {
System.out.println(“其他字段…”);
}
} else {//文件上传
String fileName=item.getName();//获取文件范围数据。路径及其名称
//判断文件类型
String ext=fileName.substring(fileName.indexOf(“.”)+1);
if(!(ext.equals(“png”)||ext.equals(“gif”)||ext.equals(“jpg”))) {
System.out.println(“仅支持png.gif,jpg类型文件”);
return ;
}
String path=request.getSession().getServletContext().getRealPath(“Upload”);//动态获取Upload文件夹位于该项目中的位置
File file=new File(path,fileName);
item.write(file);//定义上传路径,多为服务器路径
System.out.println(fileName+”上传成功”);
}
item.delete();//删除临时目录的文件
}
}
catch(FileUploadBase.SizeLimitExceededException e) {
System.out.println(“最大20KB”);
}
catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}