使用MultipartFile对象生成多个相同的文件

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

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());
          }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值