1. 利用springboot实现文件的上传
1.1利用springMVC实现文件上传的步骤:
利用SpringMVC中提供的工具API,实现文件上传的简化.
记住类型:MultipartFile
实现步骤:
1.接收资源文件
2.准备文件上传目录
3.准备文件上传的全路径 目录/文件名称
1.2前端代码的编写
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>实现文件长传</h1>
<!--enctype="开启多媒体标签" -->
<form action="http://localhost:8091/file" method="post"
enctype="multipart/form-data">
<input name="fileImage" type="file" />
<input type="submit" value="提交"/>
</form>
</body>
</html>
1.3 后端controller的编写
@RequestMapping("file")
public String file(MultipartFile fileImage){
String fileDirPAth = "D:\\SogouDownload\\JT-soft\\workspace\\images";
File dirFile = new File(fileDirPAth);
//判断文件是否存在
if (!dirFile.exists()){
//如果文件没有则应该新建目录
dirFile.mkdirs(); //创建多级目录
}
//准备文件的全路径名称
String fileName = fileImage.getOriginalFilename(); //文件的名称.后缀 如:lvyou.jgp
File realFile = new File(fileDirPAth+"/"+fileName);
//将自己信息输出到文件中
try {
fileImage.transferTo(realFile);
return "文件上传成功";
} catch (IOException e) {
e.printStackTrace();
return "文件上传失败";
}
}
2 图片的上传与回显
2.1 控制层代码
@RequestMapping("路径名称")
public ImageVo upload(MultipartFile uploadFile){
return fileService.upload(uploadFile);
}
2.2 业务层的编写
业务层的接口
package com.jt.service;
import com.jt.vo.ImageVo;
import org.springframework.web.multipart.MultipartFile;
public interface FileService {
ImageVo upload(MultipartFile uploadFile);
}
业务层的实现
package com.jt.service;
import com.jt.vo.ImageVo;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Service
@PropertySource("classpath:/properties/image.properties")
public class FileServiceImpl implements FileService {
/**
* @param uploadFile
* @return
*/
private static Set<String> imageTypeSet = new HashSet<>();
@Value("${image.localDirPath}")
private String localDirPath;
@Value("${image.urlPath}")
private String urlPath;
static {
imageTypeSet.add(".jpg");
imageTypeSet.add(".png");
imageTypeSet.add(".gif");
}
@Override
public ImageVo upload(MultipartFile uploadFile) {
//校验文件是的有效性:
//1.1 获取图片的名称
String fileName = uploadFile.getOriginalFilename();
fileName = fileName.toLowerCase();
//1.2 获取图片的类型
int index = fileName.lastIndexOf(".");
String imageType = fileName.substring(index);
if (!imageTypeSet.contains(imageType)) {//如果类型不匹配
return ImageVo.fail();
}
//校验文件是否为恶意程序 文件是否有图片的特有属性
//2.1将上传的文件类型利用图片的api进行转化,如果转化成功,则一定图片,否则不是图片
try {
BufferedImage bufferedImage = ImageIO.read(uploadFile.getInputStream());
//校验图片的特有属性,width,length
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (width == 0 || height == 0) { //如果width,length其中的一个的值为0 ,
return ImageVo.fail();
}
} catch (IOException e) {
e.printStackTrace();
return ImageVo.fail();
}
//提高用户的检索的效率 分目录存储
// 方案一:利用hash之后每隔2-3位之后拼接
// 方案二:以时间为单位进行分割 /yyyy/MM/dd/
//利用工具API将时间转化指定的格式
String datePath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
// 3.2 动态的生成文件目录 2部分 根目录 + 时间目录
String localDir = localDirPath + datePath;
//3.3 判断目录是否存在,如果不存在则新建目录
File dirFile = new File(localDir);
if (!dirFile.exists()){
dirFile.mkdirs();
}
//为了防止重名图片的提交 需要自定义文件名称 UUID\
//生成uuid
String uuid = UUID.randomUUID().toString().replace("-","");
String uuidFileName = uuid + fileName;
//实现图片的物理上传的虚拟路径
String realFilePath = localDir + uuidFileName;
File imageFile = new File(realFilePath);
try {
uploadFile.transferTo(imageFile);
} catch (IOException e) {
e.printStackTrace();
return ImageVo.fail();
}
// String url = "https://img14.360buyimg.com/n5/jfs/t1/124362/8/" +
// "7814/98425/5f194fc1E473af68a/0f3e77052d76b759.jpg";
String url = urlPath + datePath + uuidFileName;
return ImageVo.success(url);
}
}
2.3 配置文件的书写
//配置图片服务器
image.localDirPath=D:/SogouDownload/JT-soft/workspace/images
image.urlPath=http://image.jt.com
2.4 nginx当中文件的编写
编辑文件如下:
server {
listen 80;
server_name image.jt.com;
##通过我们的网址转指定的目录
location / {
root D:/SogouDownload/JT-soft/workspace/images;
}
}
在系统的host文件中添加127.0.0.1 image.jt.com