protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收普通数据
String desc = req.getParameter("desc");
//接收文件
Part part = req.getPart("upload");
//获得文件大小
long size = part.getSize();
System.out.println("文件大小:"+size);
String name = part.getName();
System.out.println("form表单名称:"+name);
//获得文件名
String header = part.getHeader("Content-Disposition");
System.out.println(header);
int idx = header.lastIndexOf("filename=\"");
String filename = header.substring(idx+10, header.length()-1);
System.out.println("文件名: "+filename);
//获得文件内容
InputStream is = part.getInputStream();
String path = this.getServletContext().getRealPath("/upload");
OutputStream os = new FileOutputStream(path+"/"+filename);
int len;
byte[] b = new byte[1024];
while((len=is.read(b))!=-1){
os.write(b, 0, len);
}
is.close();
os.close();
NO2号 |