本文主要是介绍下载文件时不能显示中文,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前段时间做了个下载图片功能,功能做完后本地测试没有任何问题,但是在Linux下却不能显示中文文件名称,纳闷了,经过反复思考,问题得以解决,特此分享,上代码
@Action(value = "download")public String download() throws IOException {// 创建Httpclient对象RequestParams requestParams = createRequestParams();String fileName = requestParams.getQueryValue("fileName");String objectNameReq = requestParams.getQueryValue("objectName");String decode = requestParams.getQueryValue("decode");if(StringUtils.isEmpty(objectNameReq)){logger.error("objectName 不能为空!");throw new NovatarRuntimeException("objectName 不能为空!");}HttpServletResponse response = ServletActionContext.getResponse();ServletOutputStream os = response.getOutputStream();HttpServletRequest request = ServletActionContext.getRequest();if(StringUtils.isNotEmpty(decode)&&decode.equals("true")){objectNameReq = SupplierUtil.decryptField(objectNameReq);//+fileName;// 判断是否为以前的nas盘if(objectNameReq.contains("/nfsc/TRTMS_GROUND/"))objectNameReq = objectNameReq.replace("/nfsc/TRTMS_GROUND/", "");if(!objectNameReq.contains(".app")){String prix = "jpg";if(!fileName.isEmpty()) prix = fileName.substring(fileName.lastIndexOf(".")).replace(".", "");objectNameReq += "/file" + "."+prix;}}else{if(StringUtils.isEmpty(fileName)){List<String> list = Arrays.asList(objectNameReq.split("/"));fileName = list.get(list.size() - 1);}fileName = StringUtils.isEmpty(fileName) ? UUID.randomUUID().toString() : fileName;objectNameReq = objectNameReq.substring(0,objectNameReq.lastIndexOf("/"))+"/"+fileName;}String objectNameReqEncoding = getEncoding(objectNameReq);if(StringUtils.isNotEmpty(objectNameReqEncoding) && !objectNameReqEncoding.equals("UTF-8")){objectNameReq = new String(objectNameReq.getBytes("UTF-8"),"UTF-8");objectNameReq = URLEncoder.encode(objectNameReq,"UTF-8");}objectNameReq = objectNameReq.replaceAll(" ", URLEncoder.encode(" ","UTF-8"));CFSClient client = new CFSClient(gwUrl);try{byte[] bytes = client.getObject(objectNameReq);/*String encoding = getEncoding(fileName);if(StringUtils.isNotEmpty(encoding) && !encoding.equals("UTF-8")){fileName = new String(fileName.getBytes("UTF-8"),"UTF-8");fileName = URLEncoder.encode(fileName,"UTF-8");}*/fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");fileName=fileName.replace('+', ' ');response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);response.setHeader("Content-Disposition", "attachment;filename= "+ fileName);os.write(bytes);response.flushBuffer();} catch (Exception e){throw new NovatarRuntimeException("文件/图片不存在");} finally {if(os != null){os.close();}}return null;}
一开始是 这样
String encoding = getEncoding(fileName);
if(StringUtils.isNotEmpty(encoding) && !encoding.equals("UTF-8")){fileName = new String(fileName.getBytes("UTF-8"),"UTF-8");fileName = URLEncoder.encode(fileName,"UTF-8");
}
理由是先判断字符类型,然后转换为utf-8,因为服务器上的字符编码也是utf-8 ,这样就不会有问题,没成想最后却是失败。
之后就改成
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
fileName=fileName.replace('+', ' ');
fileName = new String(fileName.getBytes(“UTF-8”), “ISO8859-1”); 这样写的理由我没找到答案,但是结果却让我惊呆了。
至于 fileName=fileName.replace(’+’, ’ '); 这个则是替换空格
这篇关于下载文件时不能显示中文的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!