bootstrap-upload上传图片

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

html页面




Insert title here







	


	












后台使用servlet3.0的part用于上传

	
	@RequestMapping("/upload")
	@ResponseBody
	public String upload(HttpServletRequest request) throws IOException, ServletException {
		
		String dir = System.getProperty("user.dir");
		
		Part p = request.getPart("photo");
		System.out.println(p.getSubmittedFileName());
		p.write(dir+File.separator+p.getSubmittedFileName());
		
		
		return "index";
	}
 @GetMapping("/refund/download")
    public void download(OrderRefund orderRefund, HttpServletResponse response) throws IOException{
        if(orderRefund.getFileNames().size()==1){//单文件下载
            downloadOneFile(orderRefund, response);
        }else {//多文件下载
            downloadMultiFile(orderRefund,response);
        }
    }
 @ResponseBody
    @RequestMapping(value = "/refund/upload",method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Msg upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String refundOrderId=request.getParameter("id");
            File fileUploadPath=new File(SystemProperties.FILE_UPLOAD_PATH +refundOrderId);
            if (!fileUploadPath.exists()){
                fileUploadPath.mkdirs();
            }
        if (ServletFileUpload.isMultipartContent(request)) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                List fileList = multipartRequest.getFiles("reportFile");
                for (MultipartFile item : fileList) {
                    String fileName = item.getOriginalFilename();       //当前上传文件全名称
                    File savedFile = new File(fileUploadPath.getAbsolutePath(), fileName);
                    item.transferTo(savedFile);
                }
            }
            return Msg.success();
    }
    /**
     *
     * @description  下载单个文件
     * @author liyhu
     * @date 2018/7/15 11:14
     * @return void
     **/
    private void downloadOneFile(OrderRefund orderRefund, HttpServletResponse response) throws IOException{
        String fileName = orderRefund.getFileNames().get(0);
        File file = new File(new File(SystemProperties.FILE_UPLOAD_PATH +
                orderRefund.getId()).getAbsolutePath(),  fileName);

        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition",
                "attachment;fileName=" +URLEncoder.encode(fileName,"UTF-8"));// 设置文件名
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            logger.error("单文件下载失败:",e);
        } finally {
            IOUtils.close(os);
            IOUtils.close(bis);
            IOUtils.close(fis);
        }
    }
    /**
     *
     * @description  打包成zip进行下载
     * @author liyhu
     * @date 2018/7/15 11:13
     * @return void
     **/
    private void downloadMultiFile(OrderRefund orderRefund, HttpServletResponse response) throws IOException{
        response.setContentType("application/x-msdownload");
        response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode("附件.zip", "utf-8"));

        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
        BufferedOutputStream bos = new BufferedOutputStream(zos);

        try{
            for(String fileName:orderRefund.getFileNames()){
                File file = new File(new File(SystemProperties.FILE_UPLOAD_PATH +orderRefund.getId()).getAbsolutePath(), fileName);
                if(file.exists()){
                    BufferedInputStream bis=null;
                    try{
                        bis = new BufferedInputStream(new FileInputStream(file));
                        zos.putNextEntry(new ZipEntry(fileName));
                        int len = 0;
                        byte[] buf = new byte[10 * 1024];
                        while( (len=bis.read(buf, 0, buf.length)) != -1){
                            bos.write(buf, 0, len);
                        }
                        bos.flush();
                    }finally {
                        IOUtils.close(bis);
                    }
                }else{
                    logger.error("文件:{}不存在",file.getAbsolutePath());
                }
            }
        }catch (IOException e){
            logger.error("多文件下载失败:",e);
        } finally{
            IOUtils.close(bos);
            IOUtils.close(zos);
        }
    }
   

 

转载于:https://my.oschina.net/u/3574106/blog/1845562

你可能感兴趣的:(c#,javascript,python,ViewUI)