文件所需要的依赖jar包
使用servlet文件上传的形式实现文件的上传
基于FileItem-------DiskFileItemFactory-----ServletFileUpload 实现
前台页面代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<!--单文件上传-->
<form action="file" method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<input type="submit">
</form>
<hr>
<!--多标签多文件上传-->
<form action="file1" method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<input type="file" name="file2">
<input type="submit">
</form>
<hr>
<!--单标签多文件上传-->
<form action="file2" method="post" enctype="multipart/form-data">
<input type="file" name="file1" multiple>
<input type="submit">
</form>
</body>
</html>
原始servlet上传文件不需要导包 使用 HttpServletRequest
@RequestMapping(value = "/uploadServlet", method = RequestMethod.POST)
public String upload(HttpServletRequest request) {
// 使用FileItem工场类创建相应工场对象
FileItemFactory factory = new DiskFileItemFactory();
// 创建servlet文件上传对象并将指定工场对象传入
ServletFileUpload fileUpload = new ServletFileUpload(factory);
// 声明文件集合
List<FileItem> parseRequest = null;
try {
// 使用servlet文件上传对象解析请求返回文件集合
parseRequest = fileUpload.parseRequest(request);
// 遍历文件对象集合 获取数据
for (FileItem fileItem : parseRequest) {
// 判断数据类型是不是普通的form表单字段
if (!fileItem.isFormField()) {
// 获取上传文件的文件名
String fileName = fileItem.getName();
// 使用上传文件创建输入流
InputStream fileStream = fileItem.getInputStream();
// 使用UUID+文件名的方式生成保存文件的名称,避免文件重名
String realFileName = UUID.randomUUID().toString() + "-" + fileName;
// 定义保存的父路径
File fileFatherPath = new File("F://Upload");
// 创建父路径 避免路径不存在保错
fileFatherPath.mkdirs();
// 创建要保存的文件
File file = new File(fileFatherPath, realFileName);
// 创建输出流
OutputStream out = new FileOutputStream(file);
// 创建字节缓存
byte[] buffer = new byte[1024];
int len = -1;
// 一次读取1kb(1024byte),返回-1表明读取完毕
while ((len = fileStream.read(buffer)) != -1) {
// 一次写入1kb(1024byte)
out.write(buffer, 0, len);
}
// 冲刷流资源
out.flush();
// 关闭流
out.close();
fileStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "yes";
}
使用springmvc形式MultipartFile类实现单文件上传
<!-- 文件上传依赖包 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 文件上传包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
使用springmvc形式MultipartFile类实现单文件上传
//使用springmvc形式MultipartFile类实现单文件上传
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(MultipartFile filePath) {//filepath前台input的name
//将前台获取数据存入指定位置
String toFile = ToFile(filePath);
System.out.println(toFile);
return "yes";
}
//将前台MultipartFile对象数据取出保存至文件
private static String ToFile(MultipartFile m) {
if (!m.isEmpty()) {
File fileFatherPath = new File("F://Upload");
String realFileName = UUID.randomUUID().toString() + "-" + m.getOriginalFilename();
// //创建父路径 避免路径不存在保错
fileFatherPath.mkdirs();
// // 创建要保存的文件
File file = new File(fileFatherPath, realFileName);
try {
m.transferTo(file);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file.getPath();
}
return null;
}
前台多input标签形式获取多文件上传进行保存
@RequestMapping(value = "/uploadMore", method = RequestMethod.POST)
public String uploadMore(MultipartFile filePath1, MultipartFile filePath2) {
String toFile = ToFile(filePath1);
String toFile2 = ToFile(filePath2);
System.out.println(toFile);
System.out.println(toFile2);
return "yes";
}
前台单input标签形式获取多文件上传进行保存
@RequestMapping(value = "/uploadMore2", method = RequestMethod.POST)
public String uploadMore2(MultipartFile[] filePath) {
for (MultipartFile multipartFile : filePath) {
String toFile = ToFile(multipartFile);
System.out.println(toFile);
}
return "yes";
}