controller方法:
/**
* 游戏下载接口
*/
@RequestMapping("/downLoadFile")
@ResponseBody
public void downLoadFile(InputObject inputObject, HttpServletResponse response){
InputStream inputStream=null;
File file=null;
String absFilePath= "";
OssFile ossFile=new OssFile();
try {
ossFile=_gameService.getOssFile(inputObject);
String oppositeFilePath= ConfigUtil.getSettings("game_resources_path");
if(ossFile.getFilePath()!=null)
absFilePath=oppositeFilePath+ossFile.getFilePath()+ossFile.getFileName()+"."+ossFile.getExtendName();
absFilePath=oppositeFilePath+ossFile.getFileName()+"."+ossFile.getExtendName();
//给绝对路径编码为可识别的字节码
file = new File(absFilePath);
inputStream=new FileInputStream(file);
// 如果文件名是中文,需要进行url编码
String fileName = URLEncoder.encode(ossFile.getFileName(),"UTF-8")+"."+ossFile.getExtendName();
// 设置下载的响应头
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
// 获取response字节流
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = inputStream.read(b)) != -1){
out.write(b, 0, len);
}
// 关闭
out.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 游戏下载接口
*/
@RequestMapping("/downLoadFile")
@ResponseBody
public void downLoadFile(InputObject inputObject, HttpServletResponse response){
InputStream inputStream=null;
File file=null;
String absFilePath= "";
OssFile ossFile=new OssFile();
try {
ossFile=_gameService.getOssFile(inputObject);
String oppositeFilePath= ConfigUtil.getSettings("game_resources_path");
if(ossFile.getFilePath()!=null)
absFilePath=oppositeFilePath+ossFile.getFilePath()+ossFile.getFileName()+"."+ossFile.getExtendName();
absFilePath=oppositeFilePath+ossFile.getFileName()+"."+ossFile.getExtendName();
//给绝对路径编码为可识别的字节码
file = new File(absFilePath);
inputStream=new FileInputStream(file);
// 如果文件名是中文,需要进行url编码
String fileName = URLEncoder.encode(ossFile.getFileName(),"UTF-8")+"."+ossFile.getExtendName();
// 设置下载的响应头
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
// 获取response字节流
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = inputStream.read(b)) != -1){
out.write(b, 0, len);
}
// 关闭
out.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}