从阿里云上下载文件
@RequestMapping(value = “downloadDatumInfo”, method = {RequestMethod.POST})
public void downloadDatumInfo(@RequestParam(value=”parmData”) String parmData,HttpServletRequest request, HttpServletResponse response) throws IOException{
Map<String, Object> map = CommonUtil.jsonToObject(parmData);
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
String fileName = (String) map.get(“fileName”);
String filePath = (String) map.get(“filePath”);
response.setHeader(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(fileName,”utf-8″));
String companyCode = filePath.substring(42, 45);
String diskName = “yunsuo”+companyCode+”/”;
String key = filePath.substring(filePath.lastIndexOf(“/”)+1,filePath.length());
OSSClient ossClient = new OSSClient(aliyunOssConfig.getEndPoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());
InputStream in = OSSUnit.getOSS2InputStream(ossClient, “yunsuo”, diskName, key);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis =new BufferedInputStream(in);
int len = 0;
while ((len = in.read(buff)) != -1) {
os.write(buff, 0, len);
}
os.flush();
os.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据key获取OSS服务器上的文件输入流
*
* @param client
* OSS客户端
* @param bucketName
* bucket名称
* @param diskName
* 文件路径
* @param key
* Bucket下的文件的路径名+文件名
*/
public static InputStream getOSS2InputStream(OSSClient client, String bucketName, String diskName, String key) {
OSSObject ossObj = client.getObject(bucketName, diskName + key);
return ossObj.getObjectContent();
}