下载文件中文件名含有汉语乱码问题解决

孤街浪徒 提交于 2019-12-02 05:38:51

最核心的部分

String fileName = new String(file.getName().replace(" ", "_").getBytes("UTF-8"), "ISO-8859-1");

 

String  fileName = new String(file.getName().replace(" ", "_").getBytes("UTF-8"), "ISO-8859-1");
@ApiOperation(value = "导出某个Code为Excel,访问网址就会弹出下载")
	@GetMapping(value = "/exportExcel/{dmCodeId}")
	public void exportExcel(@PathVariable("dmCodeId") int dmCodeId,HttpServletResponse response) throws UnsupportedEncodingException {
		//1 .准备数据
		DmCode dmCode=dmCodeServiceImpl.queryInfoByNatrualKey(dmCodeId);
		DmCodeValue dmCodeValue = new DmCodeValue();
		dmCodeValue.setCodeId(dmCodeId);
		List<DmCodeValue> dmCodeValues = dmCodeValueServiceImpl.searchInfos(dmCodeValue);
		dmCodeValues = dmCodeValueServiceImpl.getTreeList(dmCodeValues);
		dmCode.setValues(dmCodeValues);

		//2. 准备文件
		String filePath = ""+dmCode.getName()+".xlsx";
		File file= new File(filePath);
		//如果文件不存在就创建文件
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		//3.在生成的文件中写入数据
		List<List<Object>> data = new ArrayList<>();
		List<Object> codeNameList = Arrays.asList(dmCode.getName());               //1
		data.add(codeNameList);
		String description=
		dmCode.getDescription()==null? "描述:暂无": "描述:"+dmCode.getDescription();
		List<Object> codeDescriptionList = Arrays.asList(description);             //2
		data.add(codeDescriptionList);
		String principle= dmCode.getPrinciple()==null?"编码原则:暂无":"编码原则"+dmCode.getPrinciple();
		List<Object> principleList = Arrays.asList(principle);                     //3
		data.add(principleList);
        List<Object> emptyList = Arrays.asList();                                  //4
		data.add(emptyList);
		//List<Object> headerList = Arrays.asList("一级码值(code_value表-name+value)","二级码值(code_value表-name+value)",
		//		                                  "描述(code_value表-description)","参考文档");  //5
		List<Object> headerList = Arrays.asList("一级码值","二级码值",
				                                  "描述","参考文档");  //5
		data.add(headerList);

		List<DmCodeValue> values = dmCode.getValues();
		for (DmCodeValue dmCodeValue1: values){
			List<Object>  firstList = Arrays.asList(dmCodeValue1.getName()+":"+dmCodeValue1.getValue(),null,dmCodeValue1.getDescription(),dmCodeValue1.getRuleName());
			data.add(firstList);
			if (dmCodeValue1.getChildren()!=null){
				for (DmCodeValue dmCodeValue2:dmCodeValue1.getChildren()){
					List<Object>  secondList = Arrays.asList(null,dmCodeValue2.getName()+":"+dmCodeValue2.getValue(),dmCodeValue2.getDescription(),dmCodeValue2.getRuleName());
					data.add(secondList);
				}
			}
		}
		//ExcelUtil.writeBySimple(filePath,data,null);


		Sheet sheet = new Sheet(1);
		sheet.setSheetName(dmCode.getName());
		ExcelUtil.writeSimpleBySheet(filePath,data,null,sheet);



		//4.提供下载功能
		if(file.exists()){
			//首先设置响应的内容格式是force-download,那么你一旦点击下载按钮就会自动下载文件了

			response.setContentType("application/force-download");
			//通过设置头信息给文件命名,也即是,在前端,文件流被接受完还原成原文件的时候会以你传递的文件名来命名
			//response.addHeader("Content-Disposition",String.format("attachment; filename=\"%s\"", file.getName()));
			//response.setCharacterEncoding("utf-8");
			//response.setContentType("application/octet-stream");
			//String file_name = new String(file.getName().getBytes(), "ISO-8859-1");//或file_name = URLEncoder.encode(file_name,"UTF-8");
			//String file_name = new String(file.getName().getBytes(), "UTF-8");//或file_name = URLEncoder.encode(file_name,"UTF-8");
			//String s = new String(file.getName().getByte("iso8859-1"), "UTF-8");
			String  fileName = new String(file.getName().replace(" ", "_").getBytes("UTF-8"), "ISO-8859-1");
			response.setHeader("Content-Disposition","attachment;filename="+fileName);

			//进行读写操作
			byte[]buffer=new byte[1024];
			FileInputStream fis=null;
			BufferedInputStream bis=null;
			try{
				fis=new FileInputStream(file);
				bis=new BufferedInputStream(fis);
				OutputStream os=response.getOutputStream();
				//从源文件中读
				int i=bis.read(buffer);
				while(i!=-1){
					//写到response的输出流中
					os.write(buffer,0,i);
					i=bis.read(buffer);
				}
			}catch (IOException e){
				e.printStackTrace();
			}finally {
				//善后工作,关闭各种流
				try {
					if(bis!=null){
						bis.close();
					}
					if(fis!=null){
						fis.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		//5.删除临时文件
		ForkJoinPool forkJoinPool=new ForkJoinPool();
		forkJoinPool.submit(new Runnable() {
			@Override
			public void run() {
				try {
					// 60 秒后删除
					Thread.sleep(60000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(file.exists()&&file.isFile()) {
					file.delete();
				}
			}
		});
	}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!