方式三:spring mvc
首先依旧是导包;不过在这里建的是maven项目,因此用的是依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
然后则是前端的页面
<body>
<form action="file/upload.do" method="post" enctype="multipart/form-data">
上传者:<input type="text" name="name" /><br/>
上传的文件:<input type="file" name="file" multiple="multiple"/><br/>
<input type="submit" value="上传" />
</form>
</body>
<!--下载-->
<a href="file/download.do">点击下载</a>
java代码部分
package com.zdl;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HeaderParam;
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.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("file")
public class TestFile {
//上传部分
@RequestMapping(path ="upload.do")
public String upload(HttpServletRequest request,@RequestParam(value="file") MultipartFile [] mfile,String name){
for (MultipartFile multipartFile : mfile) {
String path = request.getServletContext().getRealPath("upload")+"\\"+multipartFile.getOriginalFilename();
System.out.println(name+"--"+path);
File file = new File(path);
try {
multipartFile.transferTo(file);
System.out.println("上传成功:"+multipartFile.getOriginalFilename());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return "success";
}
//下载部分
@RequestMapping(path="download.do")
public ResponseEntity<byte[]> download(@RequestHeader("User-Agent") String agent) throws Exception{
File file = new File("C:\\Users\\Administrator\\Desktop\\like.png");
//设置响应的说明和额外的参数
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//由注解@RequestHeader("User-Agent")获取到浏览器的类型来解决中文乱码的问题(因为浏览器的兼容性不同)
String filename = "";
if (agent.toUpperCase().contains("CHROME")) {
filename = new String(file.getName().getBytes("utf-8"),"iso-8859-1");
}else if (agent.toUpperCase().contains("MSIE")) {
filename = new String(file.getName().getBytes("iso-8859-1"),"utf-8");
}
headers.setContentDispositionFormData("attachment", filename);
//将文件转成byte数组
byte[] by = FileUtils.readFileToByteArray(file);
//三个参数分别是一个文件的byte数组,头部信息以及响应码(201)
ResponseEntity<byte[]> rn = new ResponseEntity<byte[]>(by,headers, HttpStatus.CREATED);
return rn;
}
}
springmvc.xml配置文件中上传必不可少的一部分
<context:component-scan base-package="com.zdl" />
<!-- 文件上传解析器:MultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760" />
</bean>