JAVA生成文件在linux下文件名乱码--解决方案
前言
在做OA项目开发过程中,经常遇到客户让导附件数据,一般我们都是将附件数据写到Linux文件夹中,然后将文件夹下载给客户,然后就会有一个问题,附件文件名乱码
解决方案:
方法中添加 System.setProperty(“sun.jnu.encoding”,“UTF-8”); 作用设置服务器编码为UTF-8
java生产文件到linux服务器大致代码如下:
package com.iflytek.api.util;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.api.util.dao.QueryFile;
import com.iflytek.oa.doc.service.DocService;
import com.iflytek.oa.util.OAPropUtil;
import weaver.common.StringUtil;
import weaver.general.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
/**
* <p>
* <code>downContractFileController</code>
* </p>
* Description:
*
* @author babayang
* @date 2022/6/16 11:53
*/
@Path("/file")
public class downContractFileController {
private static final DocService docService = new DocService();
private static final QueryFile dao = new QueryFile();
/**
* java写文件到Linux服务器
*
* @author 巴巴羊
* @date 2021/1/8 15:56
*/
@POST
@Path("exportFile")
@Produces(MediaType.TEXT_PLAIN)
public static String downFile(@Context HttpServletRequest request,
@Context HttpServletResponse response) {
try {
/**设置服务器编码,用于解决*/ System.setProperty("sun.jnu.encoding","UTF-8");
//获取业务表的附件id
List<Map<String, String>> dataList = dao.getCgContractInfo();
if (dataList.size() > 0) {
for (Map<String, String> map : dataList) {
fjIds = map.get("fileId");
if (StringUtil.isNotNull(fjIds)) {
for(String fileId: fileIds)
//拼接文件服务器附件地址
String fileUrl = docUrlPreFix + "/oa/oaFileDownload?f_weaver_belongto_userid=1&f_weaver_belongto_usertype=null&fileid=" + fileId + "&download=1&requestid=" + requestid + "&desrequestid=0";
//原生URL读取附件数据,附件如果是链接地址,不是固定的盘符路径地址,要么用原生的URL读取,用么用HttpClient读取,要么用okHttpClient读取
/**原始URL根据附件链接地址读取附件数据*/
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
/**设置超时间为3秒*/ conn.setConnectTimeout(3 * 1000);
/**防止屏蔽程序抓取而返回403错误*/ conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//将文件流转化为byte数组
byte[] getData = readInputStream(inputStream);
//将附件写到Linux系统
String fileName = map.get("fileName ");
//指定文件路径
File file = new File("/app/contract/"+fileName);
//写文件数据
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return JSONObject.toJSONString("downFile end...");
}
/**
** InputStream 文件流转化为byte数组
**/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}
System.setProperty(“sun.jnu.encoding”,“UTF-8”);设置后,ftp工具查看还是乱码,设置ftp的编码gbk,然后再看就正常了,就可以打包下载