Form表单多文件上传(H5和Android && Java后台)

1.配置tomcat虚拟路径:
(1)新建文件夹:D:/uploadFiles
(2 )修改tomcat的server.xml文件
下新增

<Context docBase="D:\uploadFiles" path="/upFiles" reloadable="true"
 debug="5" crossContext="true"/> 

(3)重启tomcat
2. html文件

<div class="form-group">
    <label for="exampleInputFile"></label> 
    <input type="file" accept="image/*" multiple="multiple" name="file_path">
</div>

3.js上传文件:

var formData = new FormData($('#uploadForm')[0]);
$.ajax({type : "Post",
            url : hostName + '/uploadFileAction',
            data : formData,
            contentType : false, 
            processData : false, 
            dataType : "text",
            xhr : function() {
                myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { 
                    myXhr.upload.addEventListener('progress',
                                                    progressHandlingFunction,false);
                }
                return myXhr; 
            },

      success : function(file_path) {
         if(file_path){
             //得到拼接后的全部Url地址
             alert("文件上传成功");
         }
      },
      error : function(err) {
          isSaving = false;
          console.log(JSON.stringify(err));
      }
});

4.java后台方法(需要引入commons-fileupload和commons-io两个jar包)

@WebServlet(urlPatterns = { "/uploadFileAction" })
public class UploadFileServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
        //String savePath = this.getServletContext().getRealPath("/uploadImgs");
        String savePath = "D:\\"+"uploadFiles";
        File saveFile = new File(savePath);
        if (!saveFile.exists()) {
            saveFile.mkdir();
        }

        // 上传时生成的临时文件保存目录
        String tempPath = this.getServletContext().getRealPath("/temp");
        File tmpFile = new File(tempPath);
        if (!tmpFile.exists()) {
            // 创建临时目录
            tmpFile.mkdir();
        }

        // 消息提示
        String message = "";
        boolean isSuccess = false;
        try {
            // 使用Apache文件上传组件处理文件上传步骤:
            // 1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // 设置工厂的缓冲区的大小,当上传的文件大小超过缓冲区的大小时,就会生成一个临时文件存放到指定的临时目录当中。
            factory.setSizeThreshold(1024 * 100);// 设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
            // 设置上传时生成的临时文件的保存目录
            factory.setRepository(tmpFile);
            // 2、创建一个文件上传解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
            // 监听文件上传进度
            upload.setProgressListener(new ProgressListener() {
                public void update(long pBytesRead, long pContentLength, int arg2) {
                    System.out.println("文件大小为:" + pContentLength + ",当前已处理:" + pBytesRead);
                    /**
                     * 文件大小为:14608,当前已处理:4096 文件大小为:14608,当前已处理:7367
                     * 文件大小为:14608,当前已处理:11419 文件大小为:14608,当前已处理:14608
                     */
                }
            });

            // 解决上传文件名的中文乱码
            upload.setHeaderEncoding("UTF-8");
            // 3、判断提交上来的数据是否是上传表单的数据
            if (!ServletFileUpload.isMultipartContent(request)) {
                // 按照传统方式获取数据
                return;
            }

            // 设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB
            upload.setFileSizeMax(1024 * 1024 * 5);
            // 设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为10MB
            upload.setSizeMax(1024 * 1024 * 10);
            // 4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
            List<FileItem> list = upload.parseRequest(request);

            int count  = list == null? 0 : list.size();
            int index = 0;
            for (FileItem item : list) {
                if (item.isFormField()) {
                    // 如果fileitem中封装的是普通输入项的数据
                    String name = item.getFieldName();
                    // 解决普通输入项的数据的中文乱码问题
                    String value = item.getString("UTF-8");
                    // value = new String(value.getBytes("iso8859-1"),"UTF-8");
                    System.out.println(name + "=" + value);

                } else {
                    // 如果fileitem中封装的是上传文件
                    // 得到上传的文件名称,

                    System.out.println("文件类型:文件");
                    String filename = item.getName();
                    System.out.println(filename);
                    if (filename == null || filename.trim().equals("")) {
                        continue;
                    }
                    // 注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:
                    // c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
                    // 处理获取到的上传文件的文件名的路径部分,只保留文件名部分
                    filename = filename.substring(filename.lastIndexOf("\\") + 1);
                    // 得到上传文件的扩展名
                    String fileExtName = filename.substring(filename.lastIndexOf("."));
                    // 如果需要限制上传的文件类型,那么可以通过文件的扩展名来判断上传的文件类型是否合法
                    System.out.println("上传的文件的扩展名是:" + fileExtName);
                    // 获取item中的上传文件的输入流
                    InputStream in = item.getInputStream();
                    // 得到文件保存的名称
                    String saveFilename = makeFileName(filename);
                    // 得到文件的保存目录
                    String realSavePath = makePath(saveFilename, savePath);
                    // 创建一个文件输出流

                    FileOutputStream out = new FileOutputStream(realSavePath + File.separator + saveFilename + fileExtName);
                    // 创建一个缓冲区
                    byte buffer[] = new byte[1024];
                    // 判断输入流中的数据是否已经读完的标识
                    int len = 0;
                    // 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
                    while ((len = in.read(buffer)) > 0) {
                        // 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\"
                        // + filename)当中
                        out.write(buffer, 0, len);
                    }
                    message += (saveFilename+fileExtName) ;

                    System.out.print(realSavePath+" aaa  "+saveFilename);

                    if(index +1 < count){
                        message += "&";
                    }
                    // 关闭输入流
                    in.close();
                    // 关闭输出流
                    out.close(); // 删除处理文件上传时生成的临时文件 //item.delete(); message =
                                    // "文件上传成功!";
                }
                index++;
            }
            isSuccess = true;
        } catch (FileUploadBase.FileSizeLimitExceededException e) {
            e.printStackTrace();
            request.setAttribute("message", "单个文件超出最大值!!!");
            request.getRequestDispatcher("/message.jsp").forward(request, response);
            return;
        } catch (FileUploadBase.SizeLimitExceededException e) {
            e.printStackTrace();
            request.setAttribute("message", "上传文件的总的大小超出限制的最大值!!!");
            request.getRequestDispatcher("/message.jsp").forward(request, response);

            return;
        } catch (Exception e) {
            message = "文件上传失败!";
            e.printStackTrace();
        }finally {
            try {
              request.getServerPort()+request.getContextPath()+"/WEB-INF/upload"; 
                if(isSuccess){
                    response.getWriter().append(message);
                    response.getWriter().close();
                }
            } catch (Exception ex) {

            }
        }  
    }

    private String makeFileName(String filename) { // 2.jpg
        //return  MD5Util.getMD5(filename  + "_" + UUID.randomUUID().toString());
        return System.currentTimeMillis()+"";
    }


    private String makePath(String filename, String savePath) {
        // 得到文件名的hashCode的值,得到的就是filename这个字符串对象在内存中的地址
        int hashcode = filename.hashCode();
        int dir1 = hashcode & 0xf; // 0--15
        int dir2 = (hashcode & 0xf0) >> 4; // 0-15
        // 构造新的保存目录 q
//      String dir = savePath + "\\" + dir1 + "\\" + dir2; // upload\2\3
                                                            // upload\3\5
        // File既可以代表文件也可以代表目录
        File file = new File(savePath);
        // 如果目录不存在
        if (!file.exists()) {
            // 创建目录
            file.mkdirs();
        }
        return savePath;
    }

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

        doGet(request, response);
    }
}

5.上传成功后,在http://localhost:port/upFiles/filename,即可下载对应文件。
6. android中上传多文件(okhttp):

  /**
     * 上传多文件
     * @param mImgUrls
     */
    public void uploadMuiltyImgs(List<String> mImgUrls, final ICallFunBack callFunBack) {
    OkHttpClient client = new OkHttpClient();
        // mImgUrls为存放图片的url集合
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);



        for (int i = 0; i <mImgUrls.size() ; i++) {
            File file = new File(mImgUrls.get(i));
            RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
            if (file.exists()) {
                builder.addFormDataPart("file", file.getName(), fileBody);
            }
        }
        MultipartBody requestBody = builder.build();
        //构建请求
        Request request = new Request.Builder()
                .url(Constant.UPLOAD_URL)//地址
                .post(requestBody)//添加请求体
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callFunBack.onFail(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callFunBack.onSuccess(response.body().string());
            }
        });
    }

    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*");

7.成功。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值