前言:
此时各位老铁们对“apache文件上传pom”都比较关心,同学们都想要知道一些“apache文件上传pom”的相关资讯。那么小编同时在网摘上网罗了一些对于“apache文件上传pom””的相关内容,希望各位老铁们能喜欢,你们一起来了解一下吧!在实际的企业开发中,文件上传是最常见的功能之一,SpringBoot集成了SpringMVC常用的功能,当然也包含了文
件上传的功能,实现起来没有太多的区别。
下面我们来讲解一下,使用SpringBoot如何实现多个文件上传操作。使用的环境是IntelliJ IDE开发工具。
开发过程如下:
1.1 配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=";
xmlns:xsi=";
xsi:schemaLocation="
;>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qianfeng.springboot</groupId>
<artifactId>fileupload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fileupload</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.2 application.properties
1.3 Controller类
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server.port=8080
package com.qianfeng.springboot.fileupload.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@Controller
public class UploadFileController {
@RequestMapping("/uploadfile")
public String uploadOneFile(){
return "uploadonefile"; // 最终由ThymeleafView处理,转发 classpath:templates
}
@RequestMapping("/uploadfiles")
public String uploadMultiFile(){
return "uploadmultifile"; // 最终由ThymeleafView处理,转发 classpath:templates
}
/**
* 上传单个文件
* @param file
* @param request
* @return
*/
@RequestMapping(value = "/uploadsimplefile", method = RequestMethod.POST)
@ResponseBody
public String uploadSimpleFile(@RequestParam("attachment") MultipartFile file,
HttpServletRequest request) {
try {
String fileDir =
request.getSession().getServletContext().getRealPath("/upload/");
System.out.println("------->>"+fileDir);
File dir = new File(fileDir);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = file.getOriginalFilename();
File upload_file = new File(fileDir + fileName);
file.transferTo(upload_file);
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
return "上传成功";
}
/**
* 上传多个文件
* @param file
* @param request
* @return
*/
@RequestMapping(value = "/uploadmultifile", method = RequestMethod.POST)
@ResponseBody
public String uploadMultiFile(@RequestParam("attachment") MultipartFile[] file,
HttpServletRequest request) {
try {
String fileDir =
request.getSession().getServletContext().getRealPath("/upload/");
System.out.println("------->>"+fileDir);
File dir = new File(fileDir);
if (!dir.exists()) {
dir.mkdirs();
}
for (int i=0;i<file.length;i++){
if (file[i]!=null){
uploadFile(fileDir,file[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
return "上传成功";
}
1.4 单个文件上传的页面
/**
*
* @param uploadDir
* @param file
* @throws IOException
*/
public void uploadFile(String uploadDir,MultipartFile file) throws IOException{
String file_name = file.getOriginalFilename();
File upload_file = new File(uploadDir+file_name);
file.transferTo(upload_file);
}
}
<!DOCTYPE html>
<html xmlns:th=";>
<head>
<title>文件上传</title>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/>
<!--引入CSS样式-->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">单个文件上传</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" method="post" enctype="multipart/form-data"
th:action="@{/uploadsimplefile}" role="form">
<div class="form-group">
<label for="attachment" class="col-sm-2 control-label">文件上传:
</label>
<div class="col-sm-5">
<input type="file" class="form-control" name="attachment"
id="attachment" placeholder="请选择邮件附件"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">上 传</button>
</div>
</div>
</form>
</div>
</div>
1.5 上传多个文件页面
</div>
<!--导入j是文件-->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th=";>
<head>
<title>文件上传</title>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/>
<!--引入CSS样式-->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">单个文件上传</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" method="post" enctype="multipart/form-data"
th:action="@{/uploadmultifile}" role="form">
<div class="form-group">
<label for="attachment1" class="col-sm-2 control-label">文件上传:
</label>
<div class="col-sm-5">
<input type="file" class="form-control" name="attachment"
id="attachment1" placeholder="请选择邮件附件"/>
</div>
</div>
<div class="form-group">
<label for="attachment2" class="col-sm-2 control-label">文件上传:
</label>
<div class="col-sm-5">
<input type="file" class="form-control" name="attachment"
id="attachment2" placeholder="请选择邮件附件"/>
</div>
</div>
<div class="form-group">
<label for="attachment3" class="col-sm-2 control-label">文件上传:
</label>
<div class="col-sm-5">
<input type="file" class="form-control" name="attachment"
id="attachment3" placeholder="请选择邮件附件"/>
</div>
1.6 测试
1.6.1 单文件上传
启动项目后,在浏览器中输入:,如下所示:
选择单个文件上传。
1.6.2 多文件上传
在浏览器中输入:,如下所示:
选择多个文件上。
标签: #apache文件上传pom