首页 > 代码库 > 下载附件

下载附件

/**
	 * 
	 * 下载文件
	 * 
	 * @param id
	 */
	@RequestMapping(value="http://www.mamicode.com/downloadFile")
	public void downloadFile(String id, HttpServletRequest request, HttpServletResponse response) {
		//文件头信息
		FileInfo fileinfo = licBO.findFileInfo(id);
		if(fileinfo == null)
		{
			return;
		}
		String fileName = fileinfo.getFilename();
		String path = licBO.findFilePath(id)+fileinfo.getFiletype();
		response.setContentType("video/mpeg4; charset=utf-8");  
	        // 定义输出文件头  
		try {
			response.setHeader("Content-Disposition", "attachment;filename=\""  
			            + URLEncoder.encode(fileName,"UTF-8") + "\"");
	        File file = new File(path);  
	        int len = (int) file.length();  
	        byte[] buf = new byte[4096];  
	        FileInputStream fis = new FileInputStream(file);  
	        OutputStream out = response.getOutputStream();  
	        len = fis.read(buf);  
	        while (len != -1) {  
	            out.write(buf, 0, len);  
	            len = fis.read(buf);  
	        }  
	        out.flush();  
	        out.close();  
	        fis.close();  

		} catch (Exception e) {
			// TODO 自动生成 catch 块
			logger.error(this, e);
		}  
	}

  前台下载

window.top.showPic(URLS.filedownloadurl+‘?id=‘+uploadobj[parentxxbh]);

  

下载附件