渣渣怕自己忘了,然后记下来

mvc的MultipartFile对象不能在一个方法里被重复使用,所以如果对一个上传的文件需要创建相同的多个文件,就需要把文件对象转成输入流进行操作,就不会报错,代码如下

String trueFileName=fileName;

                     // 获得输入流:  
                     InputStream picture=null;
                     try {
                      picture = file.getInputStream();
              } catch (IOException e3) {
              e3.printStackTrace();
              }    
                     // 设置存放图片文件的路径
                     path=servletPath+"\\"+advertise.getUsername()+"\\"+Adorderid+"\\"+city1+"\\"+area1+"\\"+dateStr+"\\";
                     System.out.println("path"+path);
                     dest=new File(path);
                     //检测是否存在目录
 
  if(!dest.exists()){
  dest.mkdirs();
  }
                     // 转存文件到指定的路径
          try {       
             
              String destPath =path + trueFileName;
              System.out.println("destPath=" + destPath);
              //真正写到磁盘上
              File file2 = new File(destPath);
              OutputStream out = new FileOutputStream(file2);          
              int length = 0;
              byte[] buf = new byte[1024];
              // in.read(buf) 每次读到的数据存放在buf 数组中
              while ((length = picture.read(buf)) != -1) {
                  //在buf数组中取出数据写到(输出流)磁盘上
                  out.write(buf, 0, length);
              }
              picture.close();
              out.close();
          } catch (Exception e) {
          System.out.println(e.toString());
          }

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐