渣渣怕自己忘了,然后记下来
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());
}