<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/file/upload">
<input type="file" name="file">
<input type="submit" name="upLoad">
form>
body>
html>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.baidu.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8">property>
bean>
beans>
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
@Controller
@RequestMapping("/file")
public class UploadController {
//设置字符编码,防止乱码
@RequestMapping(value = "/upload",produces = "application/json;charset = utf-8") @ResponseBody
public String upload(@RequestParam ("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
request.setCharacterEncoding("utf-8");
String filename = file.getOriginalFilename();
if ("".equals(filename)){
return "redirect:/index.jsp";
}
System.out.println("上传文件名:"+ filename);
String path = request.getServletContext().getRealPath("/upload");
File file1 = new File(path);
if (!file1.exists()){
file1.mkdir();
}
System.out.println("文件上传保存地址:"+ file1);
InputStream is = file.getInputStream();
// 我这里上传到了target目录下的uoload文件夹中
OutputStream os = new FileOutputStream(new File(file1,filename));
int len = 0;
byte []b = new byte[1024];
while ((len = is.read(b)) != -1){
os.write(b,0,len);
os.flush();
}
os.close();
is.close();
return "文件上传成功!";
}
}
前端页面不变,只需要改controller层代码即可。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@Controller
@RequestMapping("/file2")
public class FileUploadController {
@RequestMapping(value = "/upload",produces = "application/json;charset = utf-8")
@ResponseBody
public String upload2(@RequestParam ("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
String path = request.getServletContext().getRealPath("/upload");
File file1 = new File(path);
if (!file1.exists()){
file1.mkdir();
}
System.out.println(file1);
file.transferTo(new File(file1 + "/" + file.getOriginalFilename()));
return "上传完毕!";
}
}
<p><a href="${pageContext.request.contextPath}/download">下载图片a>p>
@RequestMapping(value = "/download",produces = "application/json;charset = utf-8")
@ResponseBody
public String download(HttpServletResponse response) throws IOException {
//要下载的图片地址
System.out.println("hahaha");
String path = "C:/Users/35158/Desktop/";
String fileName = "1.jpg";
File file = new File(path,fileName);
//设置响应头的信息
response.reset();//让页面不缓存
response.setCharacterEncoding("utf-8");
//二进制流传输数据
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
FileInputStream is = new FileInputStream(file);
ServletOutputStream os = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1){
os.write(buffer,0,len);
os.flush();
}
os.close();
is.close();
return null;
}