Springboot文件上传

之前讲过了ssm部分的文件上传,springboot中的上传和之前大同小异,现在总结一下。

依赖

我们之前使用springmvc进行文件上传主要用到了一下两个依赖,但是在springboot中这两个依赖都被自动装配引入了,我们就不需要重复引入。

1
2
3
4
5
6
7
8
9
10
11
12
    <!--文件上传依赖-->
<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.8.0</version>
</dependency>
</dependencies>

commons-fileupload:处理HTTP请求中的文件上传。它提供了一种简单而灵活的方式来解析HTTP请求中的多部分表单数据,包括文件上传。通过使用FileUpload,Spring MVC能够轻松地处理包含文件上传的HTTP请求,并将文件数据转换为MultipartFile对象,以便在应用程序中进一步处理。

commons-io:提供了一组实用的工具类,用于处理I/O操作。在Spring MVC文件上传中,Apache Commons IO的作用是提供了一些方便的工具类,用于处理文件和流的操作。例如,它提供了用于复制、移动、删除文件的工具类,以及用于读取和写入流的工具类。这些工具类能够简化文件操作和流操作的编码,使开发人员能够更轻松地处理文件上传和其他I/O操作。

Apache Commons IO是一个Java库,提供了一组实用的工具类,用于简化文件和流的操作。它的主要作用包括文件操作、流操作、文件过滤、文件比较等。下面是一些Apache Commons IO库的主要用途和示例说明:

  1. 文件操作: Apache Commons IO提供了一些实用的工具类,用于处理文件的操作,例如复制、移动、删除文件等。示例代码如下:

    1
    2
    3
    4
    // 复制文件
    File srcFile = new File("source.txt");
    File destFile = new File("destination.txt");
    FileUtils.copyFile(srcFile, destFile);
  2. 流操作: Apache Commons IO包含了一些实用的工具类,用于处理输入输出流的操作,例如读取、写入、复制流等。示例代码如下:

    1
    2
    3
    // 读取文件内容
    File file = new File("example.txt");
    String fileContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
  3. 文件过滤: Apache Commons IO提供了文件过滤器的功能,用于筛选文件。例如,可以根据文件名、文件类型等条件进行文件过滤。示例代码如下:

    1
    2
    3
    4
    // 筛选指定类型的文件
    File directory = new File("path/to/directory");
    String[] extensions = new String[] { "txt", "csv" };
    Collection<File> txtFiles = FileUtils.listFiles(directory, extensions, true);
  4. 文件比较: Apache Commons IO还提供了文件比较的功能,用于比较文件的内容或属性。示例代码如下:

    1
    2
    3
    4
    // 比较两个文件的内容
    File file1 = new File("file1.txt");
    File file2 = new File("file2.txt");
    boolean contentEqual = FileUtils.contentEquals(file1, file2);

综上所述,Apache Commons IO主要用于简化文件和流的操作,包括文件操作、流操作、文件过滤、文件比较等。通过使用Apache Commons IO库,开发人员可以更轻松地处理文件和流的操作,提高开发效率。

因此我们只需要导入jersy-client库来将文件上传到另一个tomcat服务器就好了。

1
2
3
4
5
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>

上传到服务器本地存储

后端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.zgy.myre.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@CrossOrigin
public class UploadController {
private static String serverURL = "http://127.0.0.1:8081/upload/";
@RequestMapping("/upload")
public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
String path = request.getServletContext().getRealPath("/upload");
File dir = new File(path);
if(!dir.exists()){
dir.mkdirs();
}
String fileName = UUID.randomUUID().toString();
String new_name = fileName.concat(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
File serv = new File(dir,new_name);
file.transferTo(serv);
return serverURL+new_name;
}
}

前端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#img_show{
width: 100px;
height: 100px;
border: black 3px solid;
}
</style>
<script src="js/jquery.min.js"></script>
<script>
let upload_file = ()=>{
console.log(123123);
let file_js = $("#file")[0].files[0];
let form_data = new FormData();
form_data.set("file",file_js);
$.ajax({
type:"post",
url:"http://127.0.0.1:8081/upload",
data:form_data,
processData: false,
contentType: false,
success(resp){
$("#img_show").attr("src",resp);
}
});
};
</script>
</head>
<body>

<form>
上传文件<input id="file" type="file" onchange="upload_file()">

</form>
文件回显:
<div class="img_show" ><img id="img_show"></div>
</body>
</html>

结果

有关上传文件夹是临时文件夹的问题

上传文件到远程服务器

配置tomcat | 塞 尔 达 是 天 (600759.xyz)](http://600759.xyz:4000/2023/12/17/文件下载-一/))

后端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.zgy.myre.controller;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@CrossOrigin
public class UploadController {
// private static String serverURL = "http://127.0.0.1:8081/upload/";
private static String serverURL = "http://127.0.0.1:8090/img/";
// @RequestMapping("/upload")
// public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
// String path = request.getServletContext().getRealPath("/upload");
// File dir = new File(path);
// if(!dir.exists()){
// dir.mkdirs();
// }
// String fileName = UUID.randomUUID().toString();
// String new_name = fileName.concat(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
// File serv = new File(dir,new_name);
// file.transferTo(serv);
// return serverURL+new_name;
//
// }
@RequestMapping("/upload")
public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
String path = request.getServletContext().getRealPath("/upload");
File dir = new File(path);
if(!dir.exists()){
dir.mkdirs();
}
String fileName = UUID.randomUUID().toString();
String new_name = fileName.concat(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
File serv = new File(dir,new_name);
Client client = Client.create();
WebResource resource = client.resource(serverURL + new_name);
System.out.println(resource.put(String.class, file.getBytes()));
return serverURL+new_name;

}
}