使⽤SpringBoot实现⽂件的上传使⽤SpringBoot实现⽂件的上传
springboot可以直接使⽤ org.springframework.web.multipart.MultipartFile
所以⾮常容易实现
⼀、⾸先是简单的单⽂件上传
先在index.html页⾯下写⼀个简单的form表单
<h1>单⽂件</h1>
<form class="form-signin" th:action="@{/SingleFile/upload}" method="post" enctype="multipart/form-data">    <p><input type="file" name="myfile"/></p>
<p><input type="submit" value="上传"/></p>
<p th:text="${result_singlefile}" th:if="${not #strings.isEmpty(result_singlefile)}"></p> </form>
注意使⽤thymeleaf
然后就到controller中写实现的代码
package ller.FileController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
@Controller
public class FileUploadController {
@PostMapping("/SingleFile/upload")
public String SingleFileUpLoad(@RequestParam("myfile") MultipartFile file, Model model) {
//判断⽂件是否为空
if(file.isEmpty()){
model.addAttribute("result_singlefile", "⽂件为空");
return "index";
}
//创建输⼊输出流
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//指定上传的位置为 d:/upload/
inputtypefile不上传文件String path = "d:/upload/";
//获取⽂件的输⼊流
inputStream = InputStream();
//获取上传时的⽂件名
String fileName = OriginalFilename();
//注意是路径+⽂件名
File targetFile = new File(path + fileName);
//如果之前的 String path = "d:/upload/" 没有在最后加 / ,那就要在 path 后⾯ + "/"
/
/判断⽂件⽗⽬录是否存在
if(!ParentFile().exists()){
//不存在就创建⼀个
}
//获取⽂件的输出流
outputStream = new FileOutputStream(targetFile);
//最后使⽤资源访问器FileCopyUtils的copy⽅法拷贝⽂件
/*参数是通过源码
public static int copy(InputStream in, OutputStream out) throws IOException {                    ......
}
⽽得知的*/
//告诉页⾯上传成功了
model.addAttribute("result_singlefile", "上传成功");
} catch (IOException e) {
e.printStackTrace();
//出现异常,则告诉页⾯失败
model.addAttribute("result_singlefile", "上传失败");
} finally {
//⽆论成功与否,都有关闭输⼊输出流
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "index";
}
步骤已在注释中说明,现在运⾏项⽬测试
选择⽂件 1.png
点击上传
成功!
⼆、多⽂件上传
与单⽂件类似,注意先遍历再执⾏
⾸先还是index.html
<h1>多⽂件</h1>
<form class="form-signin" th:action="@{/MultiFile/upload}" method="post" enctype="multipart/form-data">    <p><input type="file" name="myfile"/></p>
<p><input type="file" name="myfile"/></p>
<p><input type="file" name="myfile"/></p>
<p><input type="submit" value="上传"/></p>
<p th:text="${result_multifile}" th:if="${not #strings.isEmpty(result_multifile)}"></p>
</form>
再在刚才的controller中配置
@PostMapping("/MultiFile/upload")
public String MultiFileUpload(Model model, HttpServletRequest request) {
List<MultipartFile> list_files=((MultipartHttpServletRequest)request).getFiles("myfile");
if(list_files.isEmpty()){
model.addAttribute("result_multifile", "⽂件为空");
return "index";
}
InputStream inputStream = null;
OutputStream outputStream = null;
String path = "d:/upload/";
for (MultipartFile file : list_files) {
try {
inputStream = InputStream();
String fileName = OriginalFilename();
File targetFile = new File(path + fileName);
if(!ParentFile().exists()){
}
outputStream = new FileOutputStream(targetFile);
model.addAttribute("result_multifile", "上传成功");
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("result_multifile", "上传失败");
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "index";
}
运⾏项⽬测试
选择1.png  a.txt  b.txt
成功!
以上就是简单的⽂件上传案例,因为只是简单的实现,所以没有将重复代码整合到utils下,⽐如关闭流的操作

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。