4 回答

TA貢獻(xiàn)1946條經(jīng)驗 獲得超4個贊
MultipartFile包含一個byte array內(nèi)容文件。您可以使用 fromgetBytes()方法。
在application.properties添加:
spring.servlet.multipart.enabled=true
或者使用YAML可以添加:
spring:
servlet:
multipart:
enabled: true
并且可以從 MultipartFile 中獲取字節(jié)數(shù)組,例如:
MultipartFile file;
//for your segment file fill from client side(in request)
byte [] content = file.getBytes();

TA貢獻(xiàn)1807條經(jīng)驗 獲得超9個贊
作為替代方法,我建議您查看社區(qū)項目Spring Content。該項目旨在完全解決您的問題,為非結(jié)構(gòu)化數(shù)據(jù)的存儲提供抽象,為您注入服務(wù)和控制器實現(xiàn),這樣您就不需要自己實現(xiàn),并允許您將上傳的內(nèi)容與 Spring Data 實體相關(guān)聯(lián)。
此外,所有 Spring Content 存儲模塊,包括 JPA,都是基于流的,因此能夠處理該byte[]方法無法處理的非常大的文件。
將其添加到您的項目中將如下所示:
pom.xml(假設(shè)是 Maven)
<!-- Java API -->
<!-- just change this depdendency if you want to store somewhere else -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-jpa-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
定義內(nèi)容存儲(與 Spring Data Repository 的概念相同):
用戶內(nèi)容存儲.java
@StoreRestResource(path="photos")
public interface UserPhotoStore extends ContentStore<User, UUID> {
}
將內(nèi)容與您的User實體關(guān)聯(lián):
@Entity(name = "users")
public class User {
@Id
@GeneratedValue
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
// Spring Content annotations
@ContentId
private UUID contentId;
@ContentLength
private Long contentLen;
@MimeType
private String mimeType;
就是這樣。UserPhotoStore本質(zhì)上是一個通用的 Spring ResourceLoader 。依賴項將導(dǎo)致 Spring Content 注入該spring-content-jpa-boot-starter 接口的基于 jpa 的實現(xiàn),并且spring-content-rest-boot-starter依賴項將導(dǎo)致 Spring Content 注入@Controller將 HTTP 請求轉(zhuǎn)發(fā)到UserPhotoStore服務(wù)方法的實現(xiàn)。
總而言之,您現(xiàn)在將擁有一個功能齊全的(POST、PUT、GET、DELETE)基于 REST 的文件服務(wù)/photoso/{userId},它將使用您UserPhotoStore在數(shù)據(jù)庫中存儲(和檢索)文件并將它們與您的用戶實體相關(guān)聯(lián)。
所以:
curl -F ‘file=@/path/to/local/file.jpg’ /photos/{userId}
將上傳/path/to/local/file.jpg,將其存儲在您的數(shù)據(jù)庫中并將其與用戶實體相關(guān)聯(lián)userId。
和:
curl /photos/{userId}
將再次檢索它。
內(nèi)容可以按照文檔(關(guān)聯(lián)模式)中描述的各種方式關(guān)聯(lián)。
高溫高壓

TA貢獻(xiàn)1884條經(jīng)驗 獲得超4個贊
Spring 不會將字節(jié)存儲在類本身中,而是強制您從中打開一個流。
文件內(nèi)容要么存儲在內(nèi)存中,要么臨時存儲在磁盤上。在任何一種情況下,如果需要,用戶負(fù)責(zé)將文件內(nèi)容復(fù)制到會話級別或持久存儲。臨時存儲將在請求處理結(jié)束時被清除。
我通常使用以下代碼將中間存儲到文件或內(nèi)存中:
private final Path rootLocation;
try (InputStream inputStream = file.getInputStream()) {
byte[] in = StreamUtils.copyToByteArray(inputStream);
//Files.copy(inputStream, this.rootLocation.resolve(filename),
// StandardCopyOption.REPLACE_EXISTING);
}
當(dāng)然,您可以使用任何您喜歡的東西來從流中創(chuàng)建 byte[]。如果你在春天,可以推薦 StreamUtils

TA貢獻(xiàn)1827條經(jīng)驗 獲得超9個贊
我可能弄錯了,但我相信 Spring 正在嘗試自動映射傳遞到您的端點的整個“用戶”對象并且失敗,因為傳入的用戶對象中沒有“照片”字段,或者它是無法將該字段自動轉(zhuǎn)換為字節(jié) []。
為了測試是否是這種情況,您可以執(zhí)行以下操作之一 1)如果傳入的 User 對象沒有照片字段,請將其從您的 User 對象中刪除 2)如果有并且它正在進(jìn)來作為 MultipartFile 然后從 byte[] 更改為 multipartfile。
我知道這些都不能幫助您將其輸入數(shù)據(jù)庫,但它會幫助您確定這是否真的是您的問題。那么我很樂意提供進(jìn)一步的幫助。
編輯
好的,我一直在努力讓它工作,最后我得到了它!請參閱下面的更改。像這樣更改控制器端點
@PostMapping
@ResponseBody
public void postUser(@ModelAttribute User user, @RequestParam("file") MultipartFile file) throws IOException {
user.setPhotoByteArray(file.getBytes());
//Do Service Call here(user)
}
我像這樣更新了 User 對象。
@Entity(name = "users")
public class User {
@Id
@GeneratedValue
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "photo")
private byte[] photoByteArray;
public User(long id, String firstName, String lastName, String email) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
就是這樣!我通過郵遞員發(fā)送帶有所有必要字段的表單數(shù)據(jù)請求來驗證這項工作,我還添加了以下幾行以將文件保存在本地目錄中,以便我可以打開它以確認(rèn)它正確處理了我傳遞的圖像進(jìn)入郵遞員
FileUtils.writeByteArrayToFile(new File("test.png"), user.getPhotoByteArray());
我還向您的實體添加了一個構(gòu)造函數(shù),當(dāng)它從模型屬性創(chuàng)建對象時,它將允許它開始為空,我可以稍后設(shè)置它。
如果您仍有問題或疑問,請告訴我。
添加回答
舉報