通过接口传输文件

时间 2021/2/23 17:55:45 加载中...

相关知识点

base64 加密
字符串和字节数组相互转换
判断文件编码类型

场景

将本地的文件通过 api 接口传给其他项目(文件就 excel 文件,并不是很大)
比如返回的接口内容如下

  1. {
  2. "code":200,
  3. "msg":"成功",
  4. "files":{
  5. "title":"报告.xls",
  6. "content":"xxxxxxxx"
  7. }
  8. }

主要问题在于 content 的生成

content 这里是文件的内容,不适合文件太大的情况
如果文件太大,则最后使用文件的 url 地址,比如 http://www.xxx.com/1.pdf

思路

将文件的字符数组进行 base64 加密,即 base64加密(文件的字节数组)

base64 加密是防止存在特殊字符导致接口调用报错。

代码如下

content 的内容使用方法 fileToByteConentWithCharater 来实现。
主要是保证读取的文本没有乱码。

fileToByteConent 和 byteContentToFile 可以实现任意类型文件的 文件转字符串 和 字符串转文件

  1. import sun.misc.BASE64Decoder;
  2. import sun.misc.BASE64Encoder;
  3. /**
  4. * 将本地文件转换成字节形式的字符串,并通过 base64 加密
  5. * @param path
  6. * @return
  7. * @throws IOException
  8. */
  9. public static String fileToByteConent(String path) throws IOException {
  10. FileInputStream fileInputStream = null;
  11. fileInputStream = new FileInputStream(path);
  12. //ByteArrayInputStream inputStream = new ByteArrayInputStream(fileInputStream);
  13. StringBuilder builder = new StringBuilder();
  14. int ch = 0;
  15. ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
  16. while ((ch = fileInputStream.read()) != -1) {
  17. arrayOutputStream.write(ch);
  18. }
  19. fileInputStream.close();
  20. String val = arrayOutputStream.toString();
  21. BASE64Encoder encoder = new BASE64Encoder();
  22. String str = encoder.encode(arrayOutputStream.toByteArray());
  23. return str;
  24. }
  25. /**
  26. * 将本地文件转换成字节形式的字符串,并通过 base64 加密
  27. * @param path
  28. * @return
  29. * @throws IOException
  30. */
  31. public static String fileToByteConentWithCharater(String path) throws IOException {
  32. FileInputStream fileInputStream = null;
  33. ByteArrayOutputStream arrayOutputStream = null;
  34. try{
  35. fileInputStream = new FileInputStream(path);
  36. //ByteArrayInputStream inputStream = new ByteArrayInputStream(fileInputStream);
  37. StringBuilder builder = new StringBuilder();
  38. int ch = 0;
  39. arrayOutputStream = new ByteArrayOutputStream();
  40. while ((ch = fileInputStream.read()) != -1) {
  41. arrayOutputStream.write(ch);
  42. }
  43. }finally {
  44. if(fileInputStream != null){
  45. fileInputStream.close();
  46. }
  47. }
  48. String charsetName = detector(path);
  49. String val = arrayOutputStream.toString(charsetName);
  50. //String abc = new String(arrayOutputStream.toByteArray(),"UTF-8");
  51. // abc.getBytes();
  52. BASE64Encoder encoder = new BASE64Encoder();
  53. //String str = encoder.encode(arrayOutputStream.toByteArray());
  54. String str = encoder.encode(val.getBytes());
  55. return str;
  56. }
  57. public static String detector(String fileName) throws IOException {
  58. String encode = null;
  59. BufferedInputStream bis = null;
  60. try {
  61. bis = new BufferedInputStream(new FileInputStream(fileName));
  62. int readSize;
  63. byte[] buffer = new byte[8 * 4096];
  64. UniversalDetector detector = new UniversalDetector(null);
  65. while ((readSize = bis.read(buffer)) > 0 && !detector.isDone()) {
  66. detector.handleData(buffer, 0, readSize);
  67. }
  68. detector.dataEnd();
  69. encode = detector.getDetectedCharset();
  70. detector.reset();
  71. } finally {
  72. if (bis != null) {
  73. try {
  74. bis.close();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80. //return encode;
  81. return encode.equalsIgnoreCase("UTF-8") ? "UTF-8" : "GBK";
  82. }
  83. /**
  84. * 将 base64 加密后的字节字符串转换成本地文件
  85. * @param path
  86. * @param content
  87. * @throws IOException
  88. */
  89. public static void byteContentToFile(String path, String content) throws IOException {
  90. BASE64Decoder decoder = new BASE64Decoder();
  91. byte[] newBy = decoder.decodeBuffer(content);
  92. FileOutputStream fileOutputStream = new FileOutputStream(path, false);
  93. fileOutputStream.write(newBy);
  94. }
扫码分享
版权说明
作者:SQBER
文章来源:http://www.sqber.com/articles/transfer-file-by-api.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。