1.SpringMVC的文件上传
1) 创建项目,完善结构,导入依赖,配置web.xml
导入依赖包文件
<!-- 配置开发SpringMVC所以来的jar包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- 配置ServletAPI依赖 -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- commons-fileupload组件 -->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
配置springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解springmvc -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置自动扫描包 -->
<context:component-scan base-package="com.demo.controller"></context:component-scan>
</beans>
配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置控制管理器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 加载springmvc配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Java上传图片的代码
@Controller
public class UploadFileTest {
private static int count = 0;
@RequestMapping("/uploads")
public ModelAndView getInFile(HttpServletRequest request){
ModelAndView mav = new ModelAndView();
//1.将当前类中的ServletContext对象转换成CommonsMultipartResolver
ServletContext servletContext = request.getSession().getServletContext();
CommonsMultipartResolver cmrs = new CommonsMultipartResolver(servletContext);
// 2. 检查HttpServletRequest是否是一个文件上传的请求。
if (cmrs.isMultipart(request)){
// 3. 将HttpServletRequest转换成文件上传请求。
MultipartHttpServletRequest multipartreq = (MultipartHttpServletRequest) request;
// 4.从文件上传请求中得到文件名称。
Iterator<String> names = multipartreq.getFileNames();
while (names.hasNext()){
//得到input中的name值
String nameatter = names.next().toString();
//根据name属性得到上传上来的文件对象。
MultipartFile multipartFile = multipartreq.getFile(nameatter);
String newfilename=""; // 保存上来的文件名称。
if (multipartFile != null){
//得到被上传上来的文件真实姓名。
String isname =multipartFile.getOriginalFilename();
//得到文件的后缀名
String lastname = isname.substring(isname.lastIndexOf('.'));
newfilename = "文件" + ++count + lastname;
//得到项目的根目录
String realPath = servletContext.getRealPath("/upload");
//创建保存文件目录
File uploadpicdir = new File(realPath);
if (!uploadpicdir.exists()){
//创建目录
uploadpicdir.mkdirs();
}
newfilename = "one.jpg";
// 组织一个保存的文件对象【文件保存目录 + 文件名称】
String pathfile = uploadpicdir.getAbsolutePath()+File.separator+newfilename;
System.out.println(pathfile);
File savefile = new File(pathfile);
try {
multipartFile.transferTo(savefile);
}catch(Exception e){
e.printStackTrace();
}
}
//得到上传成功以后的文件的http访问路径
String reqURL = request.getRequestURL().toString();
reqURL = reqURL.substring(0,reqURL.lastIndexOf('/'));
reqURL =reqURL + "/upload/"+newfilename;
// http://127.0.0.1:8080/upload/xxxx.png
System.out.println("reqURL==" + reqURL);
}
mav.setViewName("successd.html");
}
return mav;
}
}
java下载图片的代码
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@Controller
public class DownLoadTest {
@RequestMapping(value = "/getfile",method = RequestMethod.GET)
public ResponseEntity<byte[]> getFile(HttpServletRequest request) throws IOException {
// 得到请求中的文件名称
String filename = request.getParameter("myfile");
// 获取绝对路径
String realPath = request.getSession().getServletContext().getRealPath("/upload");
// 创建保存文件额目录的文件对象
File uploaddir = new File(realPath);
//创建被下载的文件对象
File file = new File(uploaddir,filename);
System.out.println(file.getAbsolutePath());
//设置http协议头
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment",filename);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
}
无奈源于不够强大