文件下载

public String download() throws Exception{
		 CsmsMeetingForm csmsMeetingForm = csmsMeetingService.get(id);
		 
		 String path = ServletActionContext.getServletContext().getRealPath(MeetingConstant.ATTACH_PATH);
		 String fileName = csmsMeetingForm.getAttachment();
		  // path是指欲下载的文件的路径。
        File file = new File(path+"/"+fileName);

        // 以流的形式下载文件。
        InputStream fis = new BufferedInputStream(new FileInputStream(path+"/"+fileName));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        getResponse().reset();
        // 设置response的Header
        getResponse().addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        getResponse().addHeader("Content-Length", "" + file.length());
        OutputStream toClient = new BufferedOutputStream(getResponse().getOutputStream());
        getResponse().setContentType("application/x-download;charset=UTF-8");
        toClient.write(buffer);
        toClient.flush();
        toClient.close();

		return null;
	}

 

你可能感兴趣的:(文件下载)