当前位置:   article > 正文

Springboot上传文件,但是使用CommonsMultipartResolver解析 并 使用 Multipartfile接收到文件后发现文件变大,且文件打不开,文件损坏_multipartfile 上传文件损坏

multipartfile 上传文件损坏

Springboot上传文件,

使用 CommonsMultipartResolver 解析

使用 Multipartfile 接收到文件后发现文件变大,且文件打不开,文件损坏

这是因为使用了自定义 CustomHttpServletRequestWrapper 包装类继承了 HttpServletRequestWrapper ,获取request 并读取了inputstream把inputsream转为string。

而文件上传时,inpustream转为string,就会出现解析出错,文件损坏。

解决:克隆inputstream 并保存 byte[] ,读取时 byte[] 转换为 inputstream ,这样可以多次读取inputstream

下面是助手类,克隆inputstream 并保存 byte[]

  1. import java.io.*;
  2. public class FileUtils {
  3. //TODO 克隆Inputstream 转换为byte[]
  4. public static byte[] cloneInputStream(InputStream inputStream){
  5. try {
  6. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  7. byte[] buffer = new byte[1024];
  8. int len;
  9. while ((len = inputStream.read(buffer)) > -1){
  10. byteArrayOutputStream.write(buffer,0,len);
  11. }
  12. byteArrayOutputStream.flush();
  13. byteArrayOutputStream.close();
  14. inputStream.close();
  15. return byteArrayOutputStream.toByteArray();
  16. }catch (IOException e){
  17. e.printStackTrace();
  18. }
  19. return null;
  20. }
  21. //TODO 使用byte[] 获取 克隆Inputstream
  22. public static InputStream getInputStream(byte[] bytes){
  23. InputStream inputStreamNew = null;
  24. if(bytes != null){
  25. inputStreamNew = new ByteArrayInputStream(bytes);
  26. }
  27. return inputStreamNew;
  28. }
  29. //TODO 使用byte[] 获取 克隆Inputstream
  30. public static ByteArrayInputStream getByteArrayInputStream(byte[] bytes){
  31. ByteArrayInputStream inputStreamNew = null;
  32. if(bytes != null){
  33. inputStreamNew = new ByteArrayInputStream(bytes);
  34. }
  35. return inputStreamNew;
  36. }
  37. }

 下面是自定义 CustomHttpServletRequestWrapper 包装类继承了 HttpServletRequestWrapper

  1. import FileUtils;
  2. import javax.servlet.ReadListener;
  3. import javax.servlet.ServletInputStream;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletRequestWrapper;
  6. import java.io.*;
  7. public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
  8. // 保存request body的数据
  9. private String body;
  10. // 保存body byte[]
  11. private byte[] bodyBytes;
  12. // 解析request的inputStream(即body)数据,转成字符串
  13. public CustomHttpServletRequestWrapper(HttpServletRequest request) {
  14. super(request);
  15. StringBuilder stringBuilder = new StringBuilder();
  16. BufferedReader bufferedReader = null;
  17. InputStream inputStream = null;
  18. try {
  19. inputStream = request.getInputStream();
  20. //TODO 克隆 inputstream 到 byte[]
  21. bodyBytes = FileUtils.cloneInputStream(inputStream);
  22. inputStream = FileUtils.getInputStream(bodyBytes);
  23. if (inputStream != null) {
  24. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  25. char[] charBuffer = new char[128];
  26. int bytesRead = -1;
  27. while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
  28. stringBuilder.append(charBuffer, 0, bytesRead);
  29. }
  30. } else {
  31. stringBuilder.append("");
  32. }
  33. } catch (IOException ex) {
  34. } finally {
  35. if (inputStream != null) {
  36. try {
  37. inputStream.close();
  38. }
  39. catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. if (bufferedReader != null) {
  44. try {
  45. bufferedReader.close();
  46. }
  47. catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. body = stringBuilder.toString();
  53. }
  54. @Override
  55. public ServletInputStream getInputStream() throws IOException {
  56. //TODO 使用 byte[] 转换为 getByteArrayInputStream , 这样文件上传时就不会出现文件损坏
  57. final ByteArrayInputStream byteArrayInputStream = FileUtils.getByteArrayInputStream(bodyBytes);
  58. ServletInputStream servletInputStream = new ServletInputStream() {
  59. @Override
  60. public boolean isFinished() {
  61. return false;
  62. }
  63. @Override
  64. public boolean isReady() {
  65. return false;
  66. }
  67. @Override
  68. public void setReadListener(ReadListener readListener) {
  69. }
  70. @Override
  71. public int read() throws IOException {
  72. return byteArrayInputStream.read();
  73. }
  74. };
  75. return servletInputStream;
  76. }
  77. @Override
  78. public BufferedReader getReader() throws IOException {
  79. return new BufferedReader(new InputStreamReader(this.getInputStream()));
  80. }
  81. public String getBody() {
  82. return this.body;
  83. }
  84. // 赋值给body字段
  85. public void setBody(String body) {
  86. this.body = body;
  87. }
  88. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/895631
推荐阅读
相关标签
  

闽ICP备14008679号