这是一个平时用到的上传图片的一个工具类.
1.首先在pom文件中添加依赖
文件上传:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2.在spring配置文件中添加以下配置项
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
3.添加上传图片所需的工具类
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
public class FileUtils {//图片上传工具类
String pathRoot=request.getSession().getServletContext().getRealPath("");//这是我的电脑项目路径,记得改
//String pathRoot= "D:\\springbootweb\\buy_book_zjs\\target\\buy_book_zjs-1.0-SNAPSHOT\\resources";
System.out.println(pathRoot);
//生成uuid作为文件名称
String uuid= UUID.randomUUID().toString().replaceAll("-","");
//获得文件类型(可以判断如果不是图片,禁止上传)
String contextType=file.getContentType();
//获取文件后缀名称
String fileName=uuid+"."+contextType.substring(contextType.indexOf("/")+1);
String path="\\resources\\image\\"+fileName;
copyInputStreamToFile(file.getInputStream(),new File("D:\\myidea\\monilianxi\\web"+path));//记得改成自己项目所在路径
file.transferTo(new File(pathRoot+path));
System.out.println(pathRoot+path);
//返回文件保存到数据库的路径
return path;
}
public static void copyInputStreamToFile(InputStream inputStream, File file) throws Exception{
if (!file.exists()) {
file.createNewFile();
}
OutputStream outputStream=new FileOutputStream(file);
byte[] arr=new byte[1024];
int len=0;
while ((len=inputStream.read(arr))!=-1){
outputStream.write(arr,0,len);
}
outputStream.close();
inputStream.close();
}
}
3.在自己的controller里添加以下方法
@RequestMapping(value = "/UpdateAudit",method = RequestMethod.POST)//方法名以及提交方式
public String UpdateAudit(@RequestParam(value = "file",required = false)MultipartFile file,HttpServletRequest request,
Review review) throws Exception {
if (!file.isEmpty()){
String picture= FileUtils.uploadFile(file,request);
house.setHouseImg(picture);//此处调用对应图片字段的set方法赋值
us.addhouse(house);//添加方法
}else {
house.setHouseImg("");//此处的review是自己的pojo属性类
}
return "redirect:/house/listHouse.action";
}
4.前台页面的一些处理
<form action="/review/addReview" enctype="multipart/form-data" method="post">//换成post的提交方式,改一下enctype 新闻标题:<input type="text" name="newsTitle"/><br/> 新闻配图:<input type="file" name="file"/><br/> 新闻标题: <textarea name="newsContent" id="content" cols="30" rows="10"></textarea> <input type="submit" value="提交"/> </form>
<img th:src="${ln.newsImage}" style="width: 90px;height: 120px;"/>//显示的时候更改一下大小