/**
* 下载服务器已存在的文件
*
* @param request
* @param response
* @param proposeFile
* @throws IOException
* @throws FileNotFoundException
*/
@Value("${uploadDir}")
String path;
@RequestMapping(value="/app/downLoadExists")
private void downloadExistsFile(HttpServletRequest request,
HttpServletResponse response,String attachmentId,long pos) throws IOException,
FileNotFoundException {
try {
Attachment attachment=service.getInfoById(attachmentId);
//SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String apkPath=path+File.separator+"attachment"+File.separator+attachment.getEntityName()+File.separator+attachment.getCreateDate()+File.separator+attachment.getAttachmentName();
RandomAccessFile raf=new RandomAccessFile(new File(apkPath),"r");
raf.seek(pos);//当前文件的开始位置
// 下载
ServletOutputStream out = response.getOutputStream();
BufferedOutputStream bufferOut = new BufferedOutputStream(out);
byte[] buffer = new byte[1 * 1024];
int length = 0;
while ((length = raf.read(buffer, 0, buffer.length)) != -1) {
bufferOut.write(buffer, 0, length);
}
bufferOut.flush();
bufferOut.close();
out.close();
raf.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
手机APP实现断点续传功能,
最新推荐文章于 2022-12-27 17:10:26 发布
本文介绍了一种从服务器下载已存在文件的方法,使用了SpringMVC框架的@RequestMapping注解处理请求,通过RandomAccessFile读取文件,并将文件内容通过ServletOutputStream发送到客户端。
摘要由CSDN通过智能技术生成