给你分享下最代码的下载代码的片段@RequestMapping(value = {"/code/{id}/download"}, method = {RequestMethod.GET})
public String download(@PathVariable("id") Long id,
@RequestHeader("User-Agent") String userAgent,
HttpServletRequest request, HttpServletResponse response,
HttpSession session, final RedirectAttributes redirectAttributes)
throws IOException {
User user = (User) session
.getAttribute(GlobalConstants.SESSION_LOGIN_USER_NAME);
Answer answer = answerService.findOneById(id);
if (answer == null) {
return "redirect:/";
}
Project project = (Project) answer.getTarget();
ModuleDesc moduleDesc = ModuleConstants.PROJECT_TYPE_DESC_MAP
.get(project.getType());
String url = moduleDesc.getUrl();
if (user == null) {
redirectAttributes.addFlashAttribute("download_error", "请登录后再下载代码");
return "redirect:/" + url + "/" + project.getUuid() + ".htm";
}
if (user == null) {
redirectAttributes.addFlashAttribute("download_error", "请登录后再下载代码");
return "redirect:/" + url + "/" + project.getUuid() + ".htm";
}
String _extendJson = answer.getExtendJson();
JSONObject extendJson = JSONObject.fromObject(_extendJson);
String fileName = extendJson
.getString(ModuleConstants.ANSWER_EXTEND_JSON_FILE_NAME);
String fileId = extendJson
.getString(ModuleConstants.ANSWER_EXTEND_JSON_FILE_ID);
int indexStart = fileName.lastIndexOf(".") + 1;
String suffix = fileName.substring(indexStart).toLowerCase();
String dir = fileId.substring(0, 3) + "/";
File codeFile = new File(codePath + dir + fileId + "." + suffix);
if (suffix.equals(ModuleConstants.CODE_TYPE_JAVA)) {
codeFile = new File(codePath + dir + fileId + "/" + fileId + "."
+ suffix);
}
// 下载成功后再扣牛币
int type = ModuleConstants.EVENT_TYPE_RULE_ANSWER_GET;
String lock = answerService.getUserSourceTypeLock(user.getId(), answer.getId(), type);
synchronized (lock) {
MethodRetrun methodRetrun = answerService.synchronizedDownloadAnswer(user, answer);
if (!methodRetrun.isSuccess()) {
String msg = methodRetrun.getMsg();
String _url = methodRetrun.getUrl();
redirectAttributes.addFlashAttribute("download_error", msg);
return "redirect:" + _url;
}
}
BufferedInputStream in = null;
OutputStream out = null;
try {
if (suffix.equals(ModuleConstants.CODE_TYPE_ZIP)) {
response.setContentType("application/x-zip-compressed");
} else if (suffix.equals(ModuleConstants.CODE_TYPE_RAR)) {
response.setContentType("application/octet-stream");
} else if (suffix.equals(ModuleConstants.CODE_TYPE_JAVA)) {
response.setContentType("text/plain;charset=UTF-8");
}
long fileLength = codeFile.length();
String length = String.valueOf(fileLength);
response.setHeader("Content_Length", length);
if (userAgent.toLowerCase().indexOf("firefox") != -1) {
fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");
} else {
fileName = URLEncoder.encode(fileName, "utf-8");
}
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
in = new BufferedInputStream(new FileInputStream(codeFile));
out = response.getOutputStream();
IOUtils.copy(in, out);
} catch (Exception e) {
logger.error("Fail to download code:" + answer + ",Exception:"
+ e.getMessage());
redirectAttributes.addFlashAttribute("download_error", "下载出错");
return "redirect:/" + url + "/" + project.getUuid() + ".htm";
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
请求地址是诸如
http://www.zuidaima.com/code/11620/download.htm
11620就是一条数据库记录,这条记录中保存有文件的信息,比如存储位置,创建时间,大小等等。