springboot 读取Blob 解析成文件
发布日期:2022-02-26 14:49:37 浏览次数:38 分类:技术文章

本文共 3630 字,大约阅读时间需要 12 分钟。

/**	 * 根据应聘者id下载对应的pdf简介	 * @param request	 * @param response	 * @param id	 * @return	 * @throws IOException	 */	@SuppressWarnings("unused")	@RequestMapping(value = "/download", method = RequestMethod.GET)	@ResponseBody	 public ResponseEntity downTemp2(HttpServletRequest request,HttpServletResponse response,Long id) throws IOException{	    	try {	    		if(id==null) {	    			return new ResponseEntity<>(Jsonoutput.jsonoutput("0011", "Id is null", ""),//系统错误							HttpStatus.INTERNAL_SERVER_ERROR);	    		}	    		 Applicants arry = this.applicantsManageService.findByid(id);		    	 //获取简历id		    	 Long fileId=arry.getFileId();		    	 //未上传简历		    	 if(arry ==null && fileId==null) {		    		return new ResponseEntity<>(Jsonoutput.jsonoutput("0011", "fileId is null", ""),//系统错误							HttpStatus.INTERNAL_SERVER_ERROR);		    	 }		       //根据id获取文本内容	            File file = newsPicService.getpic(fileId);	            if(file ==null) {	            	return new ResponseEntity<>(Jsonoutput.jsonoutput("0011", "file is null", ""),//系统错误							HttpStatus.INTERNAL_SERVER_ERROR);	            }	            String fileName = file.getFileName()+"."+file.getFileType();//单独拿出来文件名,方便用	            BufferedInputStream bis = null;	            BufferedOutputStream bos = null;	            OutputStream fos = null;	            InputStream fis = null;	            try{	            	//获取blog文件流	            	byte[] contents = file.getFileData();	                fis = new ByteArrayInputStream(contents);	                bis = new BufferedInputStream(fis);	                fos = response.getOutputStream();	                bos = new BufferedOutputStream(fos);	                setFileDownloadHeader(request,response, fileName);//这个单独写一个方法,这个是防止下载的文件名乱码用的	                response.setHeader("Content-Type",file.getFileType());	                int byteRead = 0;	                byte[] buffer = new byte[8192];	                //二进制读取	                while((byteRead=bis.read(buffer,0,8192))!=-1){	                    bos.write(buffer,0,byteRead);	                }	                bos.flush();	                fis.close();	                bis.close();	                fos.close();	                bos.close();	            }catch(IOException e){	                e.printStackTrace();	            }catch (SQLException e) {	                e.printStackTrace();	            }finally{	                fos.close();	                fis.close();	            }				return null; 	    	}catch(Exception e) {				e.printStackTrace();				logger.error(e.getMessage(), e);				return new ResponseEntity<>(Jsonoutput.jsonoutput("0011", "system error", ""),//系统错误						HttpStatus.INTERNAL_SERVER_ERROR);			}	}	 //防止乱码     public static void setFileDownloadHeader(HttpServletRequest request,HttpServletResponse response, String fileName) throws SQLException, UnsupportedEncodingException {            	   //中文文件名支持				   String encodedfileName = null;				   String agent = request.getHeader("USER-AGENT");				   if(null != agent && -1 != agent.indexOf("MSIE")){//IE				       encodedfileName = java.net.URLEncoder.encode(fileName,"UTF-8");				   }else if(null != agent && -1 != agent.indexOf("Mozilla")){				       encodedfileName = new String (fileName.getBytes("GBK"),"iso-8859-1");				   }else{				       encodedfileName = java.net.URLEncoder.encode(fileName,"UTF-8");				   }				   //下面提供两种写法,根据不同情况选择使用,一般下载使用第一种				   //点击会提供对话框选择另存为				   response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName +"\"");				  				   //response.setHeader( “Content-Disposition “, “inline;filename= “+fliename)//通过IE浏览器直接选择相关应用程序插件打开    	  }

前端a链接就可以直接下载

转载地址:https://blog.csdn.net/qq_31752605/article/details/82115640 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:moment 时间过滤器简单demo
下一篇:textarea 剩余字数demo样例

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月14日 03时11分57秒