import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
public class FileUpLoad {
public static String upload(String parentPath, MultipartFile fileImage){
if (fileImage == null) {
return null;
}
InputStream is = null;
String fileName = null;
FileOutputStream os = null;
try {
is = fileImage.getInputStream();
fileName = fileImage.getOriginalFilename();
fileName = UUID.randomUUID().toString().replaceAll("-","") + fileName;
File file = new File(parentPath, fileName);
os = new FileOutputStream(new File(parentPath, fileName));
IOUtils.copy(is,os);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName;
}
}
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="5242880"></property>
</bean>