码迷,mamicode.com
首页 > Web开发 > 详细

案例34-文件上传

时间:2018-02-15 15:57:34      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:while   cep   doctype   Enctype   图片   dog   orm   eal   row   

1 案例分析

1 文件上传原理分析

技术分享图片

2 代码实现

1 form.html

 vf<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/WEB32/fileuploadServlet2" method="post" enctype="multipart/form-data">
        <input type="text" name="username"><br>
        <input type="file" name="filename"><br>
        <input type="submit" value="上传文件">
    </form>
</body>
</html>

2 FileuploadServlet代码

public class FileuploadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        
        try {
            //接受上传文件
            
            //1、创建磁盘文件项工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、创建文件上传的核心类
            ServletFileUpload upload = new ServletFileUpload(factory);
            
            //3、解析request---获得文件项集合
            List<FileItem> parseRequest = upload.parseRequest(request);

            //4、遍历文件项集合
            for(FileItem item : parseRequest){
                //5、判断普通表单项/文件上传项
                boolean formField = item.isFormField();//是否是一个普通表单项
                if(formField){
                    //普通表单项
                    String fieldName = item.getFieldName();
                    String fieldValue = item.getString();
                    System.out.println(fieldName+":"+fieldValue);
                }else{
                    //文件上传项
                    //获得上传文件的名称
                    String fileName = item.getName();
                    //获得上传文件的内容
                    InputStream in = item.getInputStream();
                    //将in中的数据拷贝服务器上
                    String path = this.getServletContext().getRealPath("upload");
                    OutputStream out = new FileOutputStream(path+"/"+fileName);
                    int len = 0;
                    byte[] buffer = new byte[1024];
                    while((len=in.read(buffer))>0){
                        out.write(buffer, 0, len);
                    }
                    
                    in.close();
                    out.close();
                }
            }
            
            
        } catch (FileUploadException e) {
            e.printStackTrace();
        }  
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

3 FileuploadServlet2代码

public class FileuploadServlet2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {

            //1、创建磁盘文件项工厂
            //作用:设置缓存文件的大小  设置临时文件存储的位置
            String path_temp = this.getServletContext().getRealPath("temp");
            //DiskFileItemFactory factory = new DiskFileItemFactory(1024*1024, new File(path_temp));
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(1024*1024);
            factory.setRepository(new File(path_temp));
            //2、创建文件上传的核心类
            ServletFileUpload upload = new ServletFileUpload(factory);
            //设置上传文件的名称的编码
            upload.setHeaderEncoding("UTF-8");

            //ServletFileUpload的API
            boolean multipartContent = upload.isMultipartContent(request);//判断表单是否是文件上传的表单
            if(multipartContent){
                //是文件上传的表单
                //***解析request获得文件项集合
                List<FileItem> parseRequest = upload.parseRequest(request);
                if(parseRequest!=null){
                    for(FileItem item : parseRequest){
                        //判断是不是一个普通表单项
                        boolean formField = item.isFormField();
                        if(formField){
                            //username=zhangsan
                            String fieldName = item.getFieldName();
                            String fieldValue = item.getString("UTF-8");//对普通表单项的内容进行编码
                            
                            System.out.println(fieldName+"----"+fieldValue);
                            
                            //当表单是enctype="multipart/form-data"时 request.getParameter相关的方法
                            //String parameter = request.getParameter("username");
                            
                        }else{
                            //文件上传项
                            //文件的名
                            String fileName = item.getName();
                            //获得上传文件的内容
                            InputStream in = item.getInputStream();
                            String path_store = this.getServletContext().getRealPath("upload");
                            OutputStream out = new FileOutputStream(path_store+"/"+fileName);
                            IOUtils.copy(in, out);
                            in.close();
                            out.close();
                            
                            //删除临时文件
                            item.delete();
                            
                        }
                    }
                }

            }else{
                //不是文件上传表单
                //使用原始的表单数据的获得方式 request.getParameter();
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }


    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

3 文件上传知识点

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

案例34-文件上传

标签:while   cep   doctype   Enctype   图片   dog   orm   eal   row   

原文地址:https://www.cnblogs.com/jepson6669/p/8449541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!