柚子快報(bào)邀請(qǐng)碼778899分享:開發(fā)語言 java——文件上傳
柚子快報(bào)邀請(qǐng)碼778899分享:開發(fā)語言 java——文件上傳
一、文件上傳——簡(jiǎn)介
文件上傳的簡(jiǎn)介:文件上傳是指將本地計(jì)算機(jī)中的文件傳輸?shù)骄W(wǎng)絡(luò)上的服務(wù)器或另一臺(tái)計(jì)算機(jī)上的過程。在 Web 開發(fā)中,文件上傳通常指的是將用戶通過 Web 頁面提交的文件(如圖像、文檔、音頻、視頻等)傳輸?shù)椒?wù)器端的操作。
簡(jiǎn)單來說,文件上傳是指將本地圖片、視頻、音頻等文件上傳到服務(wù)器上,供其他用戶瀏覽或下載的過程。 文件上傳通常涉及以下幾個(gè)主要組件:
客戶端:指的是文件上傳的發(fā)起方,通常是用戶在 Web 瀏覽器中通過表單提交文件。
服務(wù)器端:指的是接收并處理文件上傳請(qǐng)求的計(jì)算機(jī)系統(tǒng)或服務(wù)器。
上傳表單:通過 HTML 表單元素來實(shí)現(xiàn)文件上傳功能。表單需要設(shè)置 enctype 屬性為 “multipart/form-data”,并包含一個(gè)文件輸入框用于選擇要上傳的文件。
文件處理邏輯:服務(wù)器端接收到文件上傳請(qǐng)求后,需要將上傳的文件保存到指定的位置,并可能進(jìn)行進(jìn)一步的處理,如文件存儲(chǔ)、文件重命名、文件格式驗(yàn)證等。
文件上傳控制器:在 Web 開發(fā)框架中,通常需要編寫文件上傳的處理邏輯,這些邏輯通常由服務(wù)器端的控制器或處理器來處理。 文件上傳在 Web 開發(fā)中非常常見,常見的應(yīng)用場(chǎng)景包括但不限于用戶頭像上傳、文件分享、數(shù)據(jù)備份等。
二、文件上傳——入門
文件上傳的步驟:
先創(chuàng)建一個(gè)springboot工程(這個(gè)常識(shí)了,不展示了)
編寫一個(gè)上傳文件的前端html文件
編寫一個(gè)controller 項(xiàng)目結(jié)構(gòu) 文件上傳前端的三要素:
請(qǐng)求方式是:post需要設(shè)置 enctype=“multipart/form-data” 屬性以支持文件上傳
編寫的controller
package com.knife.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class UploadController {
@PostMapping("/upload")
public void upload(MultipartFile file){
System.out.println("file == " + file);
}
}
運(yùn)行截圖:(通過xml配置的路徑去訪問html文件,然后點(diǎn)擊上傳文件) file內(nèi)容不等于null,說明文件上傳成功。
總結(jié):服務(wù)端要想接收到從前端頁面上傳的文件,需要使用到一個(gè)api:MultipartFile,通過該api來接收上傳的文件。而上傳上來的文件是一個(gè)臨時(shí)文件,當(dāng)文件上傳這次請(qǐng)求完成之后,這些臨時(shí)文件會(huì)自動(dòng)刪除。因此,在上傳文件的同時(shí),應(yīng)該把文件保存起來。
三、文件上傳——本地存儲(chǔ)—第一版
在服務(wù)端,接收到上傳上來的文件之后,將文件存儲(chǔ)在本地服務(wù)器磁盤中。 基礎(chǔ)實(shí)現(xiàn):在上面的文件上傳——入門的基礎(chǔ)上,修改一下controller就行。
package com.knife.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
public class UploadController {
@PostMapping("/upload")
public void upload(MultipartFile file) throws IOException {
System.out.println("file == " + file);
// 獲取源文件的文件名
String originalFilename = file.getOriginalFilename();
// 把上傳上來的文件存儲(chǔ)到磁盤上:transferTo的參數(shù)是一個(gè)文件
file.transferTo(new File("E:\\images\\"+ originalFilename));
}
}
只需把上面的controller改成上面一樣,然后運(yùn)行,然后如果你的路徑不對(duì),或者你盤符的目錄不存在,就會(huì)報(bào)錯(cuò)了(因?yàn)檫@里沒有對(duì)異常處理,只是簡(jiǎn)單地把異常拋出去了) 最快的解決辦法就是,修改本地存儲(chǔ)的路徑,輸入一個(gè)存在的目錄的路徑。
最佳解決辦法:如下更改controller
package com.knife.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@RestController
public class UploadController {
@PostMapping("/upload")
public void upload(MultipartFile file) throws IOException {
System.out.println("file == " + file);
// 1. 獲取源文件的文件名
String originalFilename = file.getOriginalFilename();
// 定義要上傳的路徑
String fileUploadPath = "E:\\images\\"+ originalFilename;
// 創(chuàng)建要上傳的文件
File uploadFile = new File(fileUploadPath);
// 判斷配置的文件目錄是否存在,若不存在則創(chuàng)建一個(gè)新的文件目錄
File parentFile = uploadFile.getParentFile();
if(!parentFile.exists()) {
parentFile.mkdirs();
}
System.out.println(uploadFile);
file.transferTo(uploadFile);
}
}
運(yùn)行:
四、文件上傳——本地存儲(chǔ)—第二版
本地存儲(chǔ)第一版存在的問題:
使用原始文件名進(jìn)行存儲(chǔ)時(shí),如果同時(shí)上傳了文件名一樣的文件,那么后面上傳的文件會(huì)把前面上傳的文件給覆蓋。
解決:保證每一個(gè)文件的文件名是唯一的(文件名不能重復(fù))
使用uuid工具(通用唯一標(biāo)識(shí)碼)——使用生成的uuid碼+原始文件名的后綴 不管文件路徑的報(bào)錯(cuò)的寫法(和上面文件上傳——本地存儲(chǔ)—第一版就只有controller的差別):
package com.knife.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@RestController
public class UploadController {
@PostMapping("/upload")
public void upload(MultipartFile file) throws IOException {
System.out.println("file == " + file);
// 1. 獲取源文件的文件名
String originalFilename = file.getOriginalFilename();
// 2. 構(gòu)造唯一的文件名 -- 使用uuid(通用唯一識(shí)別碼,長(zhǎng)度固定的字符串,而且是唯一的)+ 原始文件文件后綴名
// 2.1. 通過字符串的截取,獲取文件的后綴名
int index = originalFilename.lastIndexOf(".");
String extname = originalFilename.substring(index);
// 2.2 把uuid和文件后綴名合并
String newFileName = UUID.randomUUID().toString() + extname;
System.out.println("newFileName = " + newFileName);
// 把上傳上來的文件存儲(chǔ)到磁盤上:(指定一個(gè)路徑)
file.transferTo(new File("E:\\"+ newFileName));
}
}
解決文件路徑的報(bào)錯(cuò)的寫法(和上面文件上傳——本地存儲(chǔ)—第一版就只有controller的差別):
package com.knife.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@RestController
public class UploadController {
@PostMapping("/upload")
public void upload(MultipartFile file) throws IOException {
System.out.println("file == " + file);
// 1. 獲取源文件的文件名
String originalFilename = file.getOriginalFilename();
// 2. 構(gòu)造唯一的文件名 -- 使用uuid(通用唯一識(shí)別碼,長(zhǎng)度固定的字符串,而且是唯一的)+ 原始文件文件后綴名
// 2.1. 通過字符串的截取,獲取文件的后綴名
int index = originalFilename.lastIndexOf(".");
String extname = originalFilename.substring(index);
// 2.2 把uuid和文件后綴名合并
String newFileName = UUID.randomUUID().toString() + extname;
// 定義要上傳的路徑
String fileUploadPath = "E:\\images\\"+ newFileName;
// 創(chuàng)建要上傳的文件
File uploadFile = new File(fileUploadPath);
// 判斷配置的文件目錄是否存在,若不存在則創(chuàng)建一個(gè)新的文件目錄
File parentFile = uploadFile.getParentFile();
if(!parentFile.exists()) {
parentFile.mkdirs();
}
System.out.println(uploadFile);
// 把上傳上來的文件存儲(chǔ)到磁盤上:(指定一個(gè)路徑)
file.transferTo(uploadFile);
}
}
運(yùn)行:
文件上傳大小限制的問題:在配置文件里面進(jìn)行配置
如果是application.properties則如上圖一樣配置就行,如果是application.yml,則如下配置即可
application.yml:
server:
port: 8088 # 配置訪問服務(wù)器的端口
spring:
servlet:
multipart:
max-file-size: 10MB # 配置單個(gè)文件最大上傳大小
max-request-size: 100MB # 配置單個(gè)請(qǐng)求最大上傳文件的大?。ㄒ淮慰梢陨蟼鞫鄠€(gè)文件,即多個(gè)文件的總和也算)
本地存儲(chǔ)存在的問題:
五、文件上傳——對(duì)象存儲(chǔ)——阿里云OOS
五、1、參照官方SDK編寫入門程序
第一步:在Maven工程中使用OSS Java SDK,只需在pom.xml中加入相應(yīng)依賴即可。在
如果使用的是Java 9及以上的版本,則需要添加JAXB相關(guān)依賴。添加JAXB相關(guān)依賴示例代碼如下:
第二步:復(fù)制上傳文件入門代碼(官方文檔)官方文檔入門程序例子:然后再根據(jù)自己的實(shí)際情況改寫
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.FileInputStream;
import java.io.InputStream;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請(qǐng)按實(shí)際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫Object完整路徑,完整路徑中不能包含Bucket名稱,例如exampledir/exampleobject.txt。
String objectName = "exampledir/exampleobject.txt";
// 填寫本地文件的完整路徑,例如D:\\localpath\\examplefile.txt。
// 如果未指定本地路徑,則默認(rèn)從示例程序所屬項(xiàng)目對(duì)應(yīng)本地路徑中上傳文件流。
String filePath= "D:\\localpath\\examplefile.txt";
// 創(chuàng)建OSSClient實(shí)例。
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
InputStream inputStream = new FileInputStream(filePath);
// 創(chuàng)建PutObjectRequest對(duì)象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
// 創(chuàng)建PutObject請(qǐng)求。
PutObjectResult result = ossClient.putObject(putObjectRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
改寫后的入門程序:主要修改自己的endpoint、accessKeyId、bucketName、objectName、filePath
package com.knife;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.FileInputStream;
import java.io.InputStream;
public class uploadOss{
public static void main(String[] args) throws Exception {
// Endpoint:(華北2(北京))請(qǐng)按實(shí)際情況填寫。
String endpoint = "oss-cn-beijing.aliyuncs.com"; // 根據(jù)自己的實(shí)際情況填寫
// 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
// EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 使用accessKeyId、accessKeyIdSecret替代。
String accessKeyId = "你自己的密鑰id"; // 根據(jù)自己的實(shí)際情況填寫
String accessKeyIdSecret ="你自己的密鑰";// 根據(jù)自己的實(shí)際情況填寫
// 填寫B(tài)ucket名稱,例如examplebucket。
String bucketName = "你自己的bucketName";// 根據(jù)自己的實(shí)際情況填寫
// 填寫Object完整路徑,完整路徑中不能包含Bucket名稱,例如exampledir/exampleobject.txt。
String objectName = "1.jpg"; //設(shè)置我們上傳的文件,最終在阿里云上面是什么名字
// 填寫本地文件的完整路徑,例如D:\\localpath\\examplefile.txt。
// 如果未指定本地路徑,則默認(rèn)從示例程序所屬項(xiàng)目對(duì)應(yīng)本地路徑中上傳文件流。
String filePath= "C:\\Users\\qq351\\Pictures\\Saved Pictures\\1.jpg";
// 創(chuàng)建OSSClient實(shí)例。
// OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeyIdSecret);
try {
InputStream inputStream = new FileInputStream(filePath);
// 創(chuàng)建PutObjectRequest對(duì)象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
// 創(chuàng)建PutObject請(qǐng)求。
PutObjectResult result = ossClient.putObject(putObjectRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
路徑獲?。? 運(yùn)行就可以傳進(jìn)去了 通過瀏覽器訪問該圖片的路徑時(shí),會(huì)直接下載該圖片,但是在頁面顯示出來挺簡(jiǎn)單的,就是使用一個(gè)imag標(biāo)簽,然后把路徑放在src下即可
六、在項(xiàng)目中使用OOS文件上傳
在項(xiàng)目中,一般會(huì)把文件上傳的方法封裝成一個(gè)工具類然后再使用。
在配置文件里面對(duì)oss的參數(shù)進(jìn)行配置
#阿里云OSS
aliyun:
oss:
endpoint: https://oss-cn-hangzhou.aliyuncs.com
accessKeyId: LTAI4GCH1vX6DKqJWxd6nEuW
accessKeySecret: yBshYweHOpqDuhCArrVHwIiBKpyqSL
bucketName: web-tlias
屬性配置類:(讀取配置文件里面的屬性)
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
封裝工具類:
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;
/**
* 阿里云 OSS 工具類
*/
@Component
public class AliOSSUtils {
@Autowired
private AliOSSProperties aliOSSProperties;
/**
* 實(shí)現(xiàn)上傳圖片到OSS
*/
public String upload(MultipartFile file) throws IOException {
//獲取阿里云OSS參數(shù)
String endpoint = aliOSSProperties.getEndpoint();
String accessKeyId = aliOSSProperties.getAccessKeyId();
String accessKeySecret = aliOSSProperties.getAccessKeySecret();
String bucketName = aliOSSProperties.getBucketName();
// 獲取上傳的文件的輸入流
InputStream inputStream = file.getInputStream();
// 避免文件覆蓋
String originalFilename = file.getOriginalFilename();
String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//上傳文件到 OSS
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, inputStream);
//文件訪問路徑
String url = endpoint.split("http://")[0] + "http://" + bucketName + "." + endpoint.split("http://")[1] + "/" + fileName;
// 關(guān)閉ossClient
ossClient.shutdown();
return url;// 把上傳到oss的路徑返回
}
}
controller類上使用
import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@RestController
public class UploadController {
@Autowired
private AliOSSUtils aliOSSUtils;
@PostMapping("/upload")
public Result upload(MultipartFile image) throws IOException {
log.info("文件上傳, 文件名: {}", image.getOriginalFilename());
//調(diào)用阿里云OSS工具類進(jìn)行文件上傳
String url = aliOSSUtils.upload(image);
log.info("文件上傳完成,文件訪問的url: {}", url);
return Result.success(url);
}
}
柚子快報(bào)邀請(qǐng)碼778899分享:開發(fā)語言 java——文件上傳
相關(guān)閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。