网页端:
Servlet:
package wodeschool.test;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Arrays;import java.util.Iterator;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;public class UploadServlet extends HttpServlet{ private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { request.setCharacterEncoding("UTF-8");//设置请求数据的编码方式 process(request,response);//文件的处理 } private void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html;charset=UTF-8"); String name=request.getParameter("name");//获取普通数据name和age String age=request.getParameter("age"); response.getWriter().write("name:"+name+"age:"+age+""+""); Part part=request.getPart("file");//getPart()用于上传单文件,getParts()用于上传多个文件。 saveUpload(part);//文件保存 response.getWriter().write("文件上传成功
"); } private void saveUpload(Part part) { // TODO Auto-generated method stub String fileName=getFileName(part);//获取文件名 fileName="school_"+System.nanoTime()+"_"+fileName;//防止文件重名 ServletContext cxt=getServletContext(); String realPath= cxt.getRealPath("/");//得到当前路径 File file=new File(realPath+"upload"); if(!file.exists()){ file.mkdir(); } File upFile=new File(file,fileName); FileOutputStream fos=null; InputStream is=null; byte[] buf=new byte[1024]; int flag=-1; try { fos=new FileOutputStream(upFile); is=part.getInputStream(); while((flag=is.read(buf,0,1024))!=-1) { fos.write(buf, 0, flag); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } finally { if(fos!=null){ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private String getFileName(Part part) { String header= part.getHeader("content-disposition"); String[] arr=header.split("filename="); return arr[arr.length-1].replace("\"", ""); }}
xml:也可以直接用注解的方式
Upload wodeschool.test.UploadServlet 1000000 1001024 4096 Upload /upload
一下为笔记部分:
envtype="multipart/form-data"是上传二进制数值的意思,如果删除它就是一个普通的表单,如果通过request.getParameter(String name)
获取值得话,获取的只是文件的名称。 //获取文件名称的方式:创建一个方法 private String getFileName(Part part){ //通过文件头 Interator<String> it=part.getHeaderNames().iterator; while(it.hasNext())//通过迭代器看文件头的内容 { String name=it.mext(); System.out.println(name+part.getHeader(name)); }//分割字符串:
String arr=header.split("filename=");//切割成数组 String fileName=arr[srr.length-1].replace("\"","");获取最后一个元素的值并去掉双引号。return fileName;
}