最近在项目开发中用到sftp来进行文件传输,最初的想法是用public OutputStream put(String dst)向该输出流中写入数据,最终将数据传输到目标服务器,结果被中文乱码困扰了好久,后面转念一想其实可以先写文件,在写文件的时候对编码进行设置,然后采用public void put(String src, String dst)直接传文件,试了一下,果真没有乱码了。
写文件:
...... String string = "字符串写入"; File destFile = new File(srcfile); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(destFile),"UTF-8"); BufferedWriter bwriter = new BufferedWriter(writer); bwriter.write(string); bwriter.close(); sftp.put(srcfile,destfile); ......
读文件:
...... InputStream in = sftp.get(destfile); JSONParser jp = new JSONParser(); JSONObject jo = (JSONObject)jp.parse(new InputStreamReader(in,"UTF-8")); ......
原文:https://www.cnblogs.com/alavender/p/8981667.html