File upload technology in JAVA-upload

File upload technology in JAVA-upload

01. Background introduction

In actual engineering projects, file uploading is often encountered. For example, after registering a website, it is necessary to upload personal photo information, and the portal website uploads public documents. In order to deal with this problem, the apache organization has also made preparations for this, providing some jar packages that are more convenient to use. The program source code will be provided at the end of the article, and of course there will be jar packages.
The javaweb environment of this blog post
write picture description here
is the tool used is eclipse, the server is Tomcat

02. Program introduction

front end:

<body>
    <div align="center">
        <!-- 一定要注意这个 enctype="multipart/form-data" method="post"-->
        <form action="/upload/demo1" enctype="multipart/form-data" method="post">
        用户名:<input type="text" name="username" /><br>
        文件 :<input type="file" name="f" /><br>
        <input type="submit" /><br>
        </form>
    </div>
</body>

Backend servlet:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //是不可以使用request.getParameter("username");获取属性的
        //创建磁盘文件项工厂 
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //创建核心上传对象
        ServletFileUpload upload = new ServletFileUpload(factory);
        //解析请求
        try {
            List<FileItem> list =upload.parseRequest(request);
            //遍历list
            for (FileItem fi : list) {
                //判断是普通的上传组件,还是文件上传组件
                if(fi.isFormField()) {//普通上传组件
                    //获取name属性
                    String name = fi.getFieldName();
                    //获取值
                    String value = fi.getString("utf-8");//这里为了测试,写的是获取普通标签的值
                    System.out.println(name+"::"+value);
                }else {
                    //文件上传组件
                    //获取name属性
                    String name = fi.getFieldName();
                    //获取文件的名称,这样处理的原因是解决不同浏览器文件名不同,可能带有路径
                    String fileName=fi.getName().substring(fi.getName().lastIndexOf("\\")+1);
                    //获取文件的内容
                    InputStream is = fi.getInputStream();
                    //获取文件的存储路径
                    String path = this.getServletContext().getRealPath("/upload/");
                    //保存文件
                    FileOutputStream os = new FileOutputStream(new File(path,fileName));
                    //将输入流写到输出流
                    IOUtils.copy(is,os);
                    //关闭资源
                    os.close();
                    is.close();
                    //删除临时文件
                    fi.delete();
                    System.out.println(fileName+"上传完毕!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

03. Program running results

write picture description here
write picture description here
write picture description here
write picture description here
write picture description here
write picture description here

04. Program source code

Link: https://pan.baidu.com/s/1nGMQvpAry-AoygDvNa7ngA Password: n4op

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326137334&siteId=291194637