从JSP下载文件

时间:2019-01-11 09:23:04

标签: java spring-mvc downloadfile

我正在尝试从JSP下载文件,但无济于事。 我创建了一个在控制器中调用方法的按钮

@RequestMapping(value = "scaricaFile", method = RequestMethod.POST)
public void getScaricaFile(HttpServletRequest request, HttpServletResponse response) {

    String nome_file = request.getParameter("nome_file");

    UploadAndDownload downloadFile = new UploadAndDownload();

    downloadFile.download(nome_file, response);
}

下载功能是

public void download(String allegato, HttpServletResponse response){

    try {
            File file = new File(path + allegato);
            response.setContentType("application/download");
            response.addHeader("Content-Disposition", "attachment; filename=" + file.getName());
            response.setContentLength((int) file.length());
            FileInputStream input = new FileInputStream(file);
            BufferedInputStream buf = new BufferedInputStream(input);
            FileCopyUtils.copy(buf, response.getOutputStream());
    } catch (IOException e) {
        System.out.println("Errore nel download del file!");
        e.printStackTrace();
    } 
}

但是在我的Chrome页面上,下载无法打开。为什么?

0 个答案:

没有答案