WEB开发文件上传与下载代码

/**
	 * 下载回避信息导入模板
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	public void downloadAvoidImportTemplete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String realPath = this.getClass().getResource("/").getPath();
		String downloadFilePath = realPath+"/com/neeq/bpm/listing/sendfile/web/internal/avoidImportTemplete.xlsx";
		String downloadFileName="回避信息导入模板.xlsx";
		CtrlUtils.downloadFile(downloadFileName, downloadFilePath, response);
	}

public static void downloadFile(String downloadFileName, String downloadFilePath,HttpServletResponse response) throws Exception{
		FileNameMap fileNameMap=URLConnection.getFileNameMap();
		//附件名称,另存为的名称
		String contentType=fileNameMap.getContentTypeFor(downloadFileName);
		if(contentType==null){
			contentType="application/unknown";
		}
		response.reset();
		response.setContentType(contentType);
		response.setHeader("Content-Disposition", "attachment;filename="+new String(downloadFileName.getBytes("gb2312"),"iso8859-1"));
		java.io.InputStream is=null;;
		OutputStream os=response.getOutputStream();
		try{
			if(!new File(downloadFilePath).exists()){throw new java.io.FileNotFoundException(downloadFilePath);}
			is=new FileInputStream(downloadFilePath);
			int count=512;
			byte[] bs=new byte[count];
			int length=-1;
			while((length=is.read(bs))!=-1){
				os.write(bs,0, length);
			}
			os.flush();
		}catch(IOException e){
			System.out.println("==========catch(IOException e):"+downloadFilePath);
			e.printStackTrace();
		}finally{
			if(is!=null)is.close();
			os.close();
		}
	}
	
	public static Map<String, Object> getMultipartFormData(HttpServletRequest request,HttpServletResponse res){
		DiskFileItemFactory  factory = new DiskFileItemFactory();
		factory.setSizeThreshold(20 * 1024 * 1024); //设定使用内存超过5M时,将产生临时文件并存储于临时目录中。   
		Map reqParaMap = new HashMap();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		try {
			List items = upload.parseRequest(request);
			Iterator itr = items.iterator();
			while (itr.hasNext()) {
				FileItem item = (FileItem) itr.next();
				String inputName=item.getFieldName();  
				if(item.isFormField()){
					String inputValue=item.getString();
					if(!StringUtils.isBlank(inputValue)){
						inputValue = new String(inputValue.getBytes("ISO-8859-1"),"utf-8");
						reqParaMap.put(inputName, inputValue);
					}
				}else if(!item.isFormField()){
					if(!StringUtils.isBlank(item.getName())){
						reqParaMap.put(inputName, item);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return reqParaMap;
    }
	public static String getReqMapPara(Map<String, Object> reqParaMap,String key){
		return reqParaMap.get(key)==null?null: (String)reqParaMap.get(key);
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值