通过接口传输文件


相关知识点
base64 加密
字符串和字节数组相互转换
判断文件编码类型
场景
将本地的文件通过 api 接口传给其他项目(文件就 excel 文件,并不是很大)
比如返回的接口内容如下
{
"code":200,
"msg":"成功",
"files":{
"title":"报告.xls",
"content":"xxxxxxxx"
}
}
主要问题在于 content 的生成
content 这里是文件的内容,不适合文件太大的情况
如果文件太大,则最后使用文件的 url 地址,比如 http://www.xxx.com/1.pdf
思路
将文件的字符数组进行 base64 加密,即 base64加密(文件的字节数组)
base64 加密是防止存在特殊字符导致接口调用报错。
代码如下
content 的内容使用方法 fileToByteConentWithCharater 来实现。
主要是保证读取的文本没有乱码。
fileToByteConent 和 byteContentToFile 可以实现任意类型文件的 文件转字符串 和 字符串转文件
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 将本地文件转换成字节形式的字符串,并通过 base64 加密
* @param path
* @return
* @throws IOException
*/
public static String fileToByteConent(String path) throws IOException {
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(path);
//ByteArrayInputStream inputStream = new ByteArrayInputStream(fileInputStream);
StringBuilder builder = new StringBuilder();
int ch = 0;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while ((ch = fileInputStream.read()) != -1) {
arrayOutputStream.write(ch);
}
fileInputStream.close();
String val = arrayOutputStream.toString();
BASE64Encoder encoder = new BASE64Encoder();
String str = encoder.encode(arrayOutputStream.toByteArray());
return str;
}
/**
* 将本地文件转换成字节形式的字符串,并通过 base64 加密
* @param path
* @return
* @throws IOException
*/
public static String fileToByteConentWithCharater(String path) throws IOException {
FileInputStream fileInputStream = null;
ByteArrayOutputStream arrayOutputStream = null;
try{
fileInputStream = new FileInputStream(path);
//ByteArrayInputStream inputStream = new ByteArrayInputStream(fileInputStream);
StringBuilder builder = new StringBuilder();
int ch = 0;
arrayOutputStream = new ByteArrayOutputStream();
while ((ch = fileInputStream.read()) != -1) {
arrayOutputStream.write(ch);
}
}finally {
if(fileInputStream != null){
fileInputStream.close();
}
}
String charsetName = detector(path);
String val = arrayOutputStream.toString(charsetName);
//String abc = new String(arrayOutputStream.toByteArray(),"UTF-8");
// abc.getBytes();
BASE64Encoder encoder = new BASE64Encoder();
//String str = encoder.encode(arrayOutputStream.toByteArray());
String str = encoder.encode(val.getBytes());
return str;
}
public static String detector(String fileName) throws IOException {
String encode = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(fileName));
int readSize;
byte[] buffer = new byte[8 * 4096];
UniversalDetector detector = new UniversalDetector(null);
while ((readSize = bis.read(buffer)) > 0 && !detector.isDone()) {
detector.handleData(buffer, 0, readSize);
}
detector.dataEnd();
encode = detector.getDetectedCharset();
detector.reset();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//return encode;
return encode.equalsIgnoreCase("UTF-8") ? "UTF-8" : "GBK";
}
/**
* 将 base64 加密后的字节字符串转换成本地文件
* @param path
* @param content
* @throws IOException
*/
public static void byteContentToFile(String path, String content) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
byte[] newBy = decoder.decodeBuffer(content);
FileOutputStream fileOutputStream = new FileOutputStream(path, false);
fileOutputStream.write(newBy);
}
扫码分享
版权说明
作者:SQBER
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
{0}
{5}
{1}
{2}回复
{4}
*昵称:
*邮箱:
个人站点:
*想说的话: