springboot MultipartFile 上传文件和下载

// 上传!!  工具类
public static String  uploadFile(byte[] file, String filePath, String fileName) throws Exception { 

        File targetFile = new File(filePath);  
    
        if(!targetFile.exists()){    
            targetFile.mkdirs();    
        }       
        
        String pathas=filePath+fileName;
        FileOutputStream out = new FileOutputStream(pathas);
       
        out.write(file);
        out.flush();
        out.close();
        
        return pathas;

    }

// 检测报告添加接口 文件上传
@RequestMapping(value="/testuploadimg", method = {RequestMethod.POST,RequestMethod.GET})
    public  String uploadImg(@RequestParam("file") MultipartFile file,HttpServletRequest request,@RequestParam("cardnum")String cardnum,@RequestParam("remark")String remark,@RequestParam("examinee")String examinee,@RequestParam("pathname")String pathname ) {
        String contentType = file.getContentType(); // 获取文件内容类型
        String fileName = file.getOriginalFilename();  // 获取上传文件名字
        
        String filePath = request.getSession().getServletContext().getRealPath("/upload/");  // 绝对路径


        System.out.println("filePath="+filePath);
//         System.out.println("filePath="+filePath);
        try {
       
            String uploadFile = HttpUtility.uploadFile(file.getBytes(), "E://upload/", fileName);
            
            String addreport = cardservice.addreport(uploadFile,cardnum,remark,examinee,pathname);
            
            return addreport;
            
        } catch (Exception e) {
            // TODO: handle exception
        }
        //返回json
        return JSON.toJSONString(ResultsUtils.error(ResultEnums.UPLOAD_FILE_ERROR));
    }


  // 文件下载
   @RequestMapping("/downreport")
   public String downreport(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
   
   String paee=request.getParameter("path");
   System.out.println("paee="+paee); 
//    String filke=request.getSession().getServletContext().getRealPath("/upload/");
//    System.out.println("filke="+filke);
   paee=paee.trim();
   String flienames=  paee.substring(paee.lastIndexOf("/")+1,paee.length());
   System.out.println("flienames="+flienames);
  
   response.setHeader("content-type", "application/octet-stream");
   response.setContentType("application/octet-stream");
   response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(flienames,"UTF-8"));
    byte[] buff = new byte[1024];
    BufferedInputStream bis = null;
    OutputStream os = null;
    try {
      os = response.getOutputStream();
      bis = new BufferedInputStream(new FileInputStream(new File(paee)));  //填写访问路径
      int i = bis.read(buff);
      while (i != -1) {
        os.write(buff, 0, buff.length);
        os.flush();
        i = bis.read(buff);
        
      }
    } catch (IOException e) {
//     e.printStackTrace();
   
    } finally {
      if (bis != null) {
        try {
          bis.close();
          
        } catch (IOException e) {
//           e.printStackTrace();
        }
      }
      if(os!=null){
      try {
os.close();
} catch (IOException e) {
// e.printStackTrace();
}
      }  
    }
    
    System.out.println("下载成功!");
  
    return null;
    
   }
   



--------------------------------------------------前端js--------------------------------------------------------------------------








   
   
   
   
   
   
   
   
   
   
 
    


   




   
   
       
       
   


   
       
       
     
   


   
       
       
   
   
       
           
   
   
       
       
   


 
   
       
       
   
上传报告:
             
   
报告名称:
             
   
卡号:
受检人:
       



       
备注:
           
       

                
       





   









本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部