Javis9548 2016-11-18 08:15 采纳率: 0%
浏览 5168

JavaWeb实现了文件下载的功能,在手机端点击下载,没有提示下载的本地,只会马上解析显示

@Controller
@RequestMapping("/file")
public class FileController {

@Value("#{configProperties['downloadPath']}")
private String downloadPath;

@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {

    String transcoding = new String(fileName.getBytes("iso8859-1"),"utf-8");
    String downloadName = URLEncoder.encode(transcoding, "UTF-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;fileName=" + downloadName);

    InputStream is = null;
    OutputStream os = null;
    try {
        String path = request.getServletContext().getContextPath();
        is = new FileInputStream(new File(downloadPath + transcoding));
        os = response.getOutputStream();
        byte[] b = new byte[2048];
        int length;
        while ((length = is.read(b)) > 0) {
            os.write(b, 0, length);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        os.close();
        is.close();
    }
}

}


就很普通的一段下载代码,在PC没问题的,但是在手机点击下载(txt/jpg),就会马上解析然后展示在页面上,现在希望是实现在手机端点击下载的时候提示下载到手机本地,之前没做过在手机端的下载,大家指点下谢谢。

  • 写回答

2条回答 默认 最新

  • zqbnqsdsmd 2017-02-22 19:40
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?