日期:2014-05-17 浏览次数:20741 次
@RequestMapping(value = "/download", method = RequestMethod.POST )
@ResponseBody
public ResponseEntity<byte[]> download(
@RequestParam("fileName") String fName) throws UnsupportedEncodingException {
System.out.println(fName);
String path = this.servletContext.getRealPath("/WEB-INF/load") + "\\aaa\\" + fName;
System.out.println(path);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", new String(fName.getBytes("GBK"),"ISO8859-1"));
try {
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(
new File(path)), headers,HttpStatus.CREATED);
} catch (IOException e) {
e.printStackTrace();
}
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "error.txt");
return new ResponseEntity<byte[]>("发送错误.".getBytes(), headers,
HttpStatus.OK);
}
<a href="<%=basePath%>upload">继续上传</a>
<c:forEach items="${files}" var="file">
<form action="download" method="post" >
<label>${file.name}</label>
<input type="submit" value="下载"/>
<input type="hidden" name ="fileName" value="${file.name}">
</form>
</c:forEach>