首先jsp页面代码:
<form method="post" action="<%= request.getContextPath()%>/user/file/upload"
enctype="multipart/form-data">
<input type="file" name="upFile">
<input type="submit" name="" value="上传">
</form>
<br/>
<a href="<%= request.getContextPath() %>/user/file/download?file3=a.txt">文件下载A</a>
这是一个maven项目使用spring框架:
上传下载地址:webapps下的download文件
1、依赖jar包:
第一个jar包用于文件上传处理,第二个jar包用于上传的文件输出到硬盘
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2、Spring.xml文件中配置file文件处理器:
<!--上传的文件解析器-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxInMemorySize" value="10485760"></property>
</bean>
3、文件上传:
有三种代码,下面全部列上,任选一种即可;
Controller层代码第一种方式:
@RequestMapping("/file/upload")
public void upload(MultipartFile upFile,HttpServletRequest request) {
//上传文件改名:
//得到上传文件安置地址的绝对路径
String uploadDir = request.getServletContext().getRealPath("download");
String newFileName = UUID.randomUUID().toString();
//用MultipartFile的对象方法得到文件名
String oldFileName = upFile.getOriginalFilename();
int index = oldFileName.lastIndexOf(".");
String extName = oldFileName.substring(index);
newFileName = newFileName + extName;
//上传文件:
FileOutputStream outputStream = null;
try {
//建立输出到硬盘指定目录的输出流
outputStream = new FileOutputStream(new File(uploadDir, newFileName));
//用commons-io jar包里的IOUtils工具输出
IOUtils.copy(upFile.getInputStream(), outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上传第二种方式:
直接用MultipartFile 对象的transfer方法。
@RequestMapping("/file/upload")
public void upload(MultipartFile upFile,HttpServletRequest request) {
//上传文件改名:
String uploadDir = request.getServletContext().getRealPath("download");
String newFileName = UUID.randomUUID().toString();
String oldFileName = upFile.getOriginalFilename();
int index = oldFileName.lastIndexOf(".");
String extName = oldFileName.substring(index);
newFileName = newFileName + extName;
//上传文件:
try {
upFile.transferTo(new File(uploadDir, newFileName));
} catch (IOException e) {
e.printStackTrace();
}finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
上传代码第三种方式:
@RequestMapping("/file/upload")
public void upload(MultipartFile upFile,HttpServletRequest request) {
InputStream inputStream =null;
try {
inputStream = upFile.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
//上传文件改名:
String uploadDir = request.getServletContext().getRealPath("download");
String newFileName = UUID.randomUUID().toString();
String oldFileName = upFile.getOriginalFilename();
int index = oldFileName.lastIndexOf(".");
String extName = oldFileName.substring(index);
newFileName = newFileName + extName;
//上传文件:
//用输出流把文件输出到download文件夹
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File(uploadDir, newFileName));
byte[] data = new byte[1024];
int len = 0;
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4、下载文件:
@RequestMapping("/file/download")
public void download(HttpServletResponse response, HttpServletRequest request) {
//取得要下载的文件名
String fileName = request.getParameter("file3");
// 约定所有下载文件在服务器上的目录位置
String download = request.getServletContext().getRealPath("download");
File file = new File(download, fileName);
// 处理下载
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
InputStream input = null;
OutputStream out = null;
try {
input = new FileInputStream(file);
out = response.getOutputStream();
byte[] data = new byte[1024];
int len = 0;
while ((len = input.read()) != -1) {
out.write(data,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
版权声明:本文为qq_38205881原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。