码上敲享录 > java常见报错解答 > java通过文件路径下载文件

java通过文件路径下载文件

上一章章节目录下一章 2019-06-18已有1431人阅读 评论(0)

java通过文件路径下载文件


解决方法:

InputStream is=null;

OutputStream os=null;

try {

   String fName = path.trim();  

String file_name = fName.substring(fName.lastIndexOf("/")+1);  

URL url=new URL(vo.getCall_record_path());

   is=url.openStream();

   response.addHeader("Content-Disposition", "attachment;filename="+file_name);

response.setContentType("application/octet-stream");

os= response.getOutputStream();

byte bf[]=new byte[1024];

int length=0;

while((length=is.read(bf, 0, 1024))!=-1){

os.write(bf, 0, length);

}

os.flush();  

} catch (IOException e) {

e.printStackTrace();

}finally {

try {

is.close();

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交