加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java 接受带有中文的get请求文件下载时的问题

发布时间:2020-12-15 01:58:31 所属栏目:Java 来源:网络整理
导读:参数是接受到了,debug的时候也能看的到,但是奇怪的是就是找不到文件 @ApiOperation(value = "文件下载/图片预览")@GetMapping(value = "/file/{type:download|view}")public Object fileDownloadOrView(ModelMap modelMap,String filePath,String fileName,@

参数是接受到了,debug的时候也能看的到,但是奇怪的是就是找不到文件

@ApiOperation(value = "文件下载/图片预览")
	@GetMapping(value = "/file/{type:download|view}")
	public Object fileDownloadOrView(ModelMap modelMap,String filePath,String fileName,@PathVariable ("type") String type,HttpServletResponse responsen ) throws Exception {
		if(StringUtils.isBlank(filePath)){
			modelMap.put("msg","参数错误!!!");
			return setModelMap(modelMap,HttpCode.BAD_REQUEST);
		}
		String note=new String(filePath.trim().getBytes("ISO-8859-1"),"UTF-8");
		File file = new File(note);
		if(file.exists() && file.isFile()){
			if("download".equals(type)){
				String fileSuffix  = file.getName().substring(file.getName().lastIndexOf("."),file.getName().length());
				if(StringUtils.isEmpty(fileName)){
					fileName = UUID.randomUUID().toString();
				} 
				fileName = fileName+fileSuffix;
				try {
					responsen.setContentType("multipart/form-data");   
					responsen.addHeader("Content-Length","" + file.length());
					responsen.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(fileName,"UTF-8"));
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
			}
			
			FileInputStream inputStream = null;
			OutputStream outputStream = null;
			try {
				 inputStream = new FileInputStream(file);
				 outputStream = responsen.getOutputStream();
				 IOUtils.copy(inputStream,outputStream);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					inputStream.close();
					outputStream.flush();
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return null;
		} else {
			modelMap.put("msg","文件不存在!!!");
			return setModelMap(modelMap,HttpCode.CONFLICT);
		}
	}

  虽然不知道为什么原因,但是?

String note=new String(filePath.trim().getBytes("ISO-8859-1"),"UTF-8");
File file = new File(note);确实吧路劲这么着的转化一下就行了

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读