web文件上传的实现

1,html页面,上传使用input type=file控件,其所在的form必须加上enctype="multipart/form-data"

    <form role="form" id="form-updUser" name="form-updUser"
                        method="post" action="updUser" enctype="multipart/form-data">
                        <div class="form-group">
                            <label for="username">用户名</label> <input type="text"
                                class="form-control" id="username" name="username"
                                placeholder="请输入用户名">
                        </div>
                        <div class="form-group">
                            <label for="name">名称</label> <input type="text"
                                class="form-control" id="name" name="name" placeholder="请输入名称">
                        </div>
                        <div class="form-group">
                            <label for="palce">归属地</label> <select id="select-province"
                                name="select-province"></select> <select id="select-city"
                                name="select-city"></select> </label>
                        </div>
                        <div class="form-group">
                            <label for="apartment">部门</label> <input type="text"
                                class="form-control" id="apartment" name="apartment"
                                placeholder="请输入部门">
                        </div>
                        <div class="form-group">
                            <label for="phoneNum">联系方式</label> <input type="text"
                                class="form-control" id="phoneNum" name="phoneNum"
                                placeholder="请输入联系方式">
                        </div>
                        <input type="hidden" name="id">
                        <div class="form-group">
                            <label for="inputfile">文件输入</label> <input type="file"
                                name="file" id="inputfile">

                        </div>
                        <input type="hidden" name="id" id="id">
                    </form>

2,后台借助于两个开源包

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

引入包之后,后台处理的代码如下

private void process(HttpServletRequest request,
            HttpServletResponse response) {
        // 进行用户更新,以及联系文件的存储
        response.setCharacterEncoding("utf-8");
        response.setContentType("html;charset=utf-8");
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding(request.getCharacterEncoding());
        try {
            List<FileItem> list = upload.parseRequest(request);
            for (int i = 0; i < list.size(); i++) {
                FileItem item = list.get(i);
                if (item.isFormField()) {
                    // 说明是普通的表单字段
            //        updUser(item);
            //        request.getRequestDispatcher("/listUser").forward(request, response);
                } else if(!item.getName().equals("")){
                    // 说明是文件
                    saveFile(item);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void saveFile(FileItem item) {
        String fileName=item.getName();
        //防止文件重名
        String imageFileName = new Date().getTime() + new Random().nextInt(1000) +fileName.substring(fileName
                   .lastIndexOf(".") );
        //网站的物理跟路径
        String rootPath=this.getServletContext().getRealPath("/");
        System.out.println("UpdUser:rootPath:"+rootPath);
        String path=rootPath+"files\";
        File dir=new File(path);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //将文件写入物理路径
        try {
            item.write(new File(path,imageFileName));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
原文地址:https://www.cnblogs.com/bobodeboke/p/4718454.html