Last active
April 22, 2016 10:20
-
-
Save KingSirLee/a7f6c1e7fbec53e12900650ca01e9053 to your computer and use it in GitHub Desktop.
文件下载
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文件流传输基础类 | |
*/ | |
public class AttachmentDTO implements Serializable { | |
private static final long serialVersionUID = -5616353779779592228L; | |
// 文件名称 | |
private String name; | |
// 文件格式 | |
private String fileExt; | |
// 文件流 | |
private byte[] fileBytes; | |
// 文件存储路径 | |
private String path; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getFileExt() { | |
return fileExt; | |
} | |
public void setFileExt(String fileExt) { | |
this.fileExt = fileExt; | |
} | |
public byte[] getFileBytes() { | |
return fileBytes; | |
} | |
public void setFileBytes(byte[] fileBytes) { | |
this.fileBytes = fileBytes; | |
} | |
public String getPath() { | |
return path; | |
} | |
public void setPath(String path) { | |
this.path = path; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class CommonController { | |
@Resource | |
private CommonProvider commonProvider; | |
@RequestMapping(value = "") | |
public void getFileByName(@RequestParam("file") String fileName, HttpServletRequest request, HttpServletResponse response) | |
throws IOException { | |
AttachmentDTO attachment = commonProvider.getFileByName(fileName); | |
// 判断文件是否存在 | |
if (attachment == null) { | |
throw new BusinessException("目标文件不存在fileName:" + fileName); | |
} | |
try { | |
response.setContentType("application/x-download"); | |
response.setContentLength((int) attachment.getFileBytes().length); | |
response.setHeader("Content-Disposition", "attachment; filename=\"" + attachment.getName() + "\""); | |
FileCopyUtils.copy(attachment.getFileBytes(), response.getOutputStream()); | |
} catch (Exception e) { | |
throw new BusinessException("目标文件读取发生异常fileName:" + fileName, e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 根据文件名获取byte流 | |
*/ | |
public AttachmentDTO getFileBytesByName(String fileName) { | |
AttachmentDTO atta = new AttachmentDTO(); | |
File file = getFileByName(fileName); | |
atta.setFileBytes(FileCopyUtils.copyToByteArray(file)); | |
atta.setName(file.getName()); | |
atta.setPath(file.getName()); | |
atta.setFileExt(FilenameUtils.getExtension(file.getName())); | |
return atta; | |
} | |
private File getFileByName(String fileName){ | |
String path = ... // 这里面是服务里文件存储目录,File.separator文件目录分隔符 | |
File file = new File(path + fileName); | |
return file; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment