实例
Spring管理文件上传解析器
- bean的Id必须为multipartResolver
- 定义最大上传量;
- 可能会定义上传的字符集编码。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
实现文件上传
@RequestMapping("/file")
public String imageFile(MultipartFile image) throws IllegalStateException, IOException{
//1.定义文件上传的目录 E:\jt-upload
File imageFile = new File("E:/jt-upload");
//2.判断文件是否存在
if(!imageFile.exists()){
//创建文件夹
imageFile.mkdirs();
}
//3.获取文件名称
String fileName = image.getOriginalFilename();
//4.实现文件上传 文件路径/文件名称
image.transferTo(new File("E:/jt-upload/"+fileName));
System.out.println("文件上传实现成功!!!!!");
//跳转到index页面
return "index";
}
项目
编辑vo对象
- 格式
{"error":0,"url":"图片的保存路径","width":图片的宽度,"height":图片的高度}
参数说明: 0代表是一张图片,如果是0,前台才可以解析并显示。1代表不是图片,
不显示如果不设置宽度和高度,则默认用图片原来的大小,所以不用设置
public class PicUploadResult{
private Integer error = 0;
private String url;
private String width;
private String height;
}
Controller
@RequestMapping("/pic/upload")
@ResponseBody
public PicUploadResult fileUpload(MultipartFile uploadFile){
return fileService.fileUpload(uploadFile);
}
Service
@Service
public class FileServiceImpl implements FileService {
private String localPath = "E:/file-upload/";
private String urlPath = "http://image.test.com/";
/**
* 1.判断图片的类型 jpg|png|git
* 2.判断是否为恶意程序
* 3.为了提高检索效率.将文件分文件存储.
* UUID:hash随机算法(当前毫秒数+算法+hash)=32位hash值 2^32=21亿
* 3.1 aaaaaaaa-bbbbbbbb-cccccccc-dddddddd/1.jpg
* 优点:几乎永远不需要修改代码
* 3.2 yyyy/MM/dd
*
* 4.如何杜绝文件重名现象
* uuid+随机数(100).jpg
* 5.实现文件上传
*/
@Override
public PicUploadResult fileUpload(MultipartFile uploadFile) {
PicUploadResult uploadResult = new PicUploadResult();
//1.判断是否为图片类型
String fileName = uploadFile.getOriginalFilename();//abc.jpg
fileName = fileName.toLowerCase(); //将字符全部转化为小写
if(!fileName.matches("^.*\\.(jpg|png|gif)$")){
uploadResult.setError(1);
return uploadResult; //文件不是图片
}
//2.判断是否为恶意程序
try {
BufferedImage bufferedImage =
ImageIO.read(uploadFile.getInputStream());
int height = bufferedImage.getHeight(); //获取图片的高度
int width = bufferedImage.getWidth(); //获取图片的宽度
if(height == 0 || width == 0){
//表示不是图片
uploadResult.setError(1);
return uploadResult;
}
//程序执行到这里 证明是一张图片
//为文件进行分文件存储 yyyy/MM/dd
String datePath =
new SimpleDateFormat("yyyy/MM/dd").format(new Date());
//定义文件保存的路径 E:/jt-upload/2018/12/12
String dirPath = localPath + datePath;
//判断文件夹是否存在
File dirFile = new File(dirPath);
if(!dirFile.exists()){
//文件不存在时,应该创建文件夹
dirFile.mkdirs();
}
//4.动态生成文件名称 UUID+三位随机数 ss-aa-cc ssaacc
String uuid = UUID.randomUUID().toString().replace("-", "");
int randomNum = new Random().nextInt(1000);
String imageFileType =
fileName.substring(fileName.lastIndexOf(".")); //.jpg
String imageFileName = uuid + randomNum + imageFileType;
//5.实现文件上传 E:/jt-upload/2018/12/12/ssdfsd.jpg
String imageLocalPath = dirPath +"/" + imageFileName;
uploadFile.transferTo(new File(imageLocalPath));
//6.封装参数
uploadResult.setHeight(height+"");
uploadResult.setWidth(width+"");
//7.准备虚拟路径 "http://image.jt.com/yyyy/MM/dd/abc.jpg";
String imageUrlPath = urlPath + datePath + "/" + imageFileName;
uploadResult.setUrl(imageUrlPath);//添加虚拟路径
} catch (Exception e) {
e.printStackTrace();
uploadResult.setError(1);
return uploadResult;
}
return uploadResult;
}
}