stmn1320
骑驴快跑
2013-12-21 19:53
浏览 258
已采纳

附件下载

附件下载,为什么点击下载的时候是把我的方法.do下载下来了,代码如下
[code="java"]
@RequestMapping
public void viewFileUpload(Long fileId, HttpServletResponse response) throws Exception{
AssetBase file = assetBaseService.findByPrimaryKey(fileId);
/*ServletOutputStream out = response.getOutputStream();
out.write(file.getFileData());
out.flush();
out.close();*/
byte[] data = file.getFileData();
response.reset();
response.setHeader("Content-Disposition", "attachment;");

response.setContentType("application/octet-stream;charset=UTF-8");

OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());

outputStream.write(data);

outputStream.flush();

outputStream.close();

}

[/code]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • c297186864
    c297186864 2013-12-23 10:44
    已采纳

    你试试这种方式
    public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
    // 下载本地文件
    String fileName = "Operator.doc".toString(); // 文件的默认保存名
    // 读到流中
    InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
    // 设置输出的格式
    response.reset();
    response.setContentType("bin");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    // 循环取出流中的数据
    byte[] b = new byte[100];
    int len;
    try {
    while ((len = inStream.read(b)) > 0)
    response.getOutputStream().write(b, 0, len);
    inStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    个人签名:[url=http://www.sdpretty.com]健康宝宝[/url]

    点赞 评论
  • iraymo
    RAYM 2013-12-23 10:45

    response.setHeader("Content-disposition", "attachment; filename="

    + new String(fileName.getBytes("utf-8"), "ISO8859-1"));

    点赞 评论

相关推荐