前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java 将文件流转化成字符串传输

java 将文件流转化成字符串传输

作者头像
用户7886150
修改2021-04-29 14:42:40
1.4K0
修改2021-04-29 14:42:40
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: Java程序将InputStream转换为字符串

摘要:1.需要文件转换成字节数组。2.将字节数组转换成字符串,不过需要注意的是需要将字节数组使用Base64加密,这样防止传输过程中因为编码问题导致文件损坏的问题。3.接收方将字符串转化成字节数组,再使用Base64解密,再输出到文件就OK了。下面贴上代码:/***summary:将字符串存储为文件采用Base64解码*@paramfileStr*@paramoutfile**/publicstaticvoidstreamSaveAsFile(InputStreamis,Strin 1. 需要文件转换成字节数组。? ?2. 将字节数组转换成字符串,不过需要注意的是需要将字节数组使用Base64加密,这样防止传输过程中因为编码问题导致文件损坏的问题。? ?3. 接收方将字符串转化成字节数组,再使用Base64解密,再输出到文件就OK了。? ?下面贴上代码:(BASE64Decoder不能使用的百度配置)?

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import sun.misc.BASE64Decoder;

public class FileStrUtil {

? ? /**

? ? ?* summary:将字符串存储为文件 采用Base64解码

? ? ?*?

? ? ?* @param fileStr

? ? ?* @param outfile

? ? ?*?

? ? ?*/

? ? public static void streamSaveAsFile(InputStream is, String outFileStr) {

? ? ? ? FileOutputStream fos = null;

? ? ? ? try {

? ? ? ? ? ? File file = new File(outFileStr);

? ? ? ? ? ? BASE64Decoder decoder = new BASE64Decoder();

? ? ? ? ? ? fos = new FileOutputStream(file);

? ? ? ? ? ? byte[] buffer = decoder.decodeBuffer(is);

? ? ? ? ? ? fos.write(buffer, 0, buffer.length);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? throw new RuntimeException(e);

? ? ? ? } finally {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? is.close();

? ? ? ? ? ? ? ? fos.close();

? ? ? ? ? ? } catch (Exception e2) {

? ? ? ? ? ? ? ? e2.printStackTrace();

? ? ? ? ? ? ? ? throw new RuntimeException(e2);

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? /**

? ? ?*?

? ? ?*?

? ? ?* summary:将字符串存储为文件

? ? ?*?

? ? ?* @param fileStr

? ? ?* @param outfile

? ? ?*?

? ? ?*/

? ? public static void stringSaveAsFile(String fileStr, String outFilePath) {

? ? ? ? InputStream out = new ByteArrayInputStream(fileStr.getBytes());

? ? ? ? FileStrUtil.streamSaveAsFile(out, outFilePath);

? ? }

? ? /**

? ? ?* 将流转换成字符串 使用Base64加密

? ? ?*?

? ? ?* @param in输入流

? ? ?* @return

? ? ?* @throws IOException

? ? ?*/

? ? public static String streamToString(InputStream inputStream) throws IOException {

? ? ? ? byte[] bt = toByteArray(inputStream);

? ? ? ? inputStream.close();

? ? ? ? String out = new sun.misc.BASE64Encoder().encodeBuffer(bt);

? ? ? ? return out;

? ? }

? ? /**

? ? ?* 将流转换成字符串

? ? ?*?

? ? ?* @param in输入流

? ? ?* @return

? ? ?* @throws IOException

? ? ?*/

? ? public static String fileToString(String filePath) throws IOException {

? ? ? ? File file = new File(filePath);

? ? ? ? FileInputStream is = new FileInputStream(file);

? ? ? ? String fileStr = FileStrUtil.streamToString(is);

? ? ? ? return fileStr;

? ? }

? ? /**

? ? ?*?

? ? ?* summary:将流转化为字节数组

? ? ?*?

? ? ?* @param inputStream

? ? ?* @return

? ? ?* @throws IOException

? ? ?*?

? ? ?*/

? ? public static byte[] toByteArray(InputStream inputStream) throws IOException {

? ? ? ? ByteArrayOutputStream out = new ByteArrayOutputStream();

? ? ? ? byte[] buffer = new byte[1024 * 4];

? ? ? ? byte[] result = null;

? ? ? ? try {

? ? ? ? ? ? int n = 0;

? ? ? ? ? ? while ((n = inputStream.read(buffer)) != -1) {

? ? ? ? ? ? ? ? out.write(buffer, 0, n);

? ? ? ? ? ? }

? ? ? ? ? ? result = out.toByteArray();

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? throw new RuntimeException(e);

? ? ? ? } finally {

? ? ? ? ? ? out.close();

? ? ? ? }

? ? ? ? return result;

? ? }

? ? public static void main(String[] args) throws Exception {

? ? ? ? String fromPath = "F://fileupload//aaa.docx";

? ? ? ? String toPath = "C://Users//Desktop//aaaa.docx";

? ? ? ? String fileStr = FileStrUtil.fileToString(fromPath);

? ? ? ? FileStrUtil.stringSaveAsFile(fileStr, toPath);

? ? }

}

本文系转载,前往查看

如有侵权,请联系?cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系?cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com