java后台简单从腾讯云下载文件通知前端以附件的形式保存
腾讯云对象存储和阿里云差不多
这是我的配置
/** * 腾讯云client * @return COSClient */ public static COSClient getCOSClient() { long appId = 00000; String secretId = ""; String secretKey = ""; // 1 初始化身份信息 COSCredentials cred = new BasicCOSCredentials(appId , secretId, secretKey); // 2 设置 Bucket 的区域, XML的区域详细信息请参见 可用地域(https://www.qcloud.com/document/product/436/6224) 文档 ClientConfig clientConfig = new ClientConfig(new Region("cos.na-toronto")); // 3 生成 cos 客户端 return new COSClient(cred, clientConfig); }
下载代码
@Autowired private COSClient cosClient; @Override public MessageVo getDownLoadFile(String fileName, String cosKey, HttpServletResponse response, HttpServletRequest request) { MessageVo messageVo = MessageVoUtil.getMessageVo(); try { // 从腾讯云下载文件 GetObjectRequest getObjectRequest = new GetObjectRequest(ManageStaticParam.COSCLIENT_BUCKET_TEST, "/yu/"+cosKey);//bucketName需要自己设置 COSObject cosObject = cosClient.getObject(getObjectRequest); // 已缓冲的方式从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取 BufferedReader reader = new BufferedReader(new InputStreamReader(cosObject .getObjectContent())); COSObjectInputStream cosObjectInput = cosObject.getObjectContent(); // 缓冲文件输出流 BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); // 通知浏览器以附件形式下载 // response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8")); response.setContentType("application/doc"); final String userAgent = request.getHeader("USER-AGENT"); if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器 fileName = URLEncoder.encode(fileName,"UTF-8"); }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器 fileName = new String(fileName.getBytes(), "ISO8859-1"); }else{ fileName = URLEncoder.encode(fileName,"UTF-8");//其他浏览器 } response.addHeader("Content-Disposition", "attachment;filename=" +fileName);//这里设置一下让浏览器弹出下载提示框,而不是直接在浏览器中打开 // 进行解码 BASE64Decoder base64Decoder = new BASE64Decoder(); byte[] car= new byte[1024]; while (true) { String line = reader.readLine(); if (line == null) break; car = base64Decoder.decodeBuffer(line); outputStream.write(car); } reader.close(); // byte[] car = new byte[1024]; // int L; // // while((L = cosObjectInput.read(car)) != -1){ // System.out.println(car+"=========="+car.length); // if (car.length!=0){ // outputStream.write(car, 0,L); // } // } outputStream.flush(); outputStream.close(); messageVo.setCode(ReturnCodeEnum.处理成功.getStatenum()); return messageVo; } catch (IOException e) { e.printStackTrace(); messageVo.setCode(ReturnCodeEnum.请求实体不存在.getStatenum()); messageVo.setData(" Backend file write error !!!"); return messageVo; } catch (CosServiceException e){ messageVo.setCode(ReturnCodeEnum.请求实体不存在.getStatenum()); messageVo.setData(" The request entity does not exist !!!"); return messageVo; } }
注意:在实际使用该方法下载的过程中,可能遇到服务器不报错,但就是下载不下来文件的问题,这样有可能是前端页面发出下载请求的方式有误,不能使用AJAX的get方式访问该方法,因为Ajax能够返回的数据格式只能为html,script,json,xml,不接受流的形式。笔者使用的方式是用window.location.href访问,或者使用from表单提交方式(GET/POST)。
腾讯开放平台文档
https://cloud.tencent.com/document/product/436/10199#.E7.AE.80.E5.8D.95.E6.96.87.E4.BB.B6.E4.B8.8A.E4.BC.A0
阿里云下载链接
http://blog.csdn.net/qq_35498405/article/details/77942817