spring上傳文件
本文將說明spring上傳文件如何配置,以及從request請求中解析到文件流的原理
#添加依賴
主要用來解析request請求流,獲取文件欄位名、上傳文件名、content-type、headers等內容組裝成FileItem
<!--添加fileupload依賴-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
#構建單例bean
CommonsMultipartResolver,將request請求從類型HttpServletRequest轉化成MultipartHttpServletRequest,從MultipartHttpServletRequest可以獲取上傳文件的各種資訊文件名、文件流等內容
注意:該bean的beanName要寫成multipartResolver,否則無法獲取到該bean
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
// 上傳限制最大位元組數 -1表示沒限制
commonsMultipartResolver.setMaxUploadSize(-1);
// 每個文件限制最大位元組數 -1表示沒限制
commonsMultipartResolver.setMaxUploadSizePerFile(-1);
commonsMultipartResolver.setDefaultEncoding(StandardCharsets.UTF_8.name());
return commonsMultipartResolver;
}
#DispatcherServlet
public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver";
private void initMultipartResolver(ApplicationContext context) {
try {
this.multipartResolver = context.getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Using MultipartResolver [" + this.multipartResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// Default is no multipart resolver.
this.multipartResolver = null;
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate MultipartResolver with name '" + MULTIPART_RESOLVER_BEAN_NAME +
"': no multipart request handling provided");
}
}
}
#校驗請求
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
// multipartResolver不為空 且 request請求頭中的content-type以multipart/開頭
if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) {
logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +
"this typically results from an additional MultipartFilter in web.xml");
}
else if (hasMultipartException(request)) {
logger.debug("Multipart resolution previously failed for current request - " +
"skipping re-resolution for undisturbed error rendering");
}
else {
try {
// 解析請求
return this.multipartResolver.resolveMultipart(request);
}
catch (MultipartException ex) {
if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) != null) {
logger.debug("Multipart resolution failed for error dispatch", ex);
// Keep processing error dispatch with regular request handle below
}
else {
throw ex;
}
}
}
}
// If not returned before: return original request.
return request;
}
#解析請求
@Override
public MultipartHttpServletRequest resolveMultipart(final HttpServletRequest request) throws MultipartException {
Assert.notNull(request, "Request must not be null");
MultipartParsingResult parsingResult = parseRequest(request);
return new DefaultMultipartHttpServletRequest(request, parsingResult.getMultipartFiles(),
parsingResult.getMultipartParameters(), parsingResult.getMultipartParameterContentTypes());
}
protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
String encoding = determineEncoding(request);
// 獲取FileUpload實例
FileUpload fileUpload = prepareFileUpload(encoding);
try {
// 將request請求解析成FileItem
List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
return parseFileItems(fileItems, encoding);
}
catch (FileUploadBase.SizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
}
catch (FileUploadBase.FileSizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getFileSizeMax(), ex);
}
catch (FileUploadException ex) {
throw new MultipartException("Failed to parse multipart servlet request", ex);
}
}
// ctx->將request進行了包裝
public List<FileItem> parseRequest(RequestContext ctx)
throws FileUploadException {
List<FileItem> items = new ArrayList<FileItem>();
boolean successful = false;
try {
// 通過ctx構建FileItem流的迭代器
FileItemIterator iter = getItemIterator(ctx);
// FileItemFactory創建FileItem的工廠對象
FileItemFactory fac = getFileItemFactory();
if (fac == null) {
throw new NullPointerException("No FileItemFactory has been set.");
}
// 判斷是否itemValid是否為true,是否有可讀文件
while (iter.hasNext()) {
final FileItemStream item = iter.next();
// Don't use getName() here to prevent an InvalidFileNameException.
// 文件名稱
final String fileName = ((FileItemIteratorImpl.FileItemStreamImpl) item).name;
// 構建FileItem
FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(),
item.isFormField(), fileName);
items.add(fileItem);
try {
// 將FileItemStreamImpl流拷貝到fileItem的輸出流中(系統會自建文件)
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
} catch (FileUploadIOException e) {
throw (FileUploadException) e.getCause();
} catch (IOException e) {
throw new IOFileUploadException(format("Processing of %s request failed. %s",
MULTIPART_FORM_DATA, e.getMessage()), e);
}
final FileItemHeaders fih = item.getHeaders();
fileItem.setHeaders(fih);
}
successful = true;
return items;
} catch (FileUploadIOException e) {
throw (FileUploadException) e.getCause();
} catch (IOException e) {
throw new FileUploadException(e.getMessage(), e);
} finally {
if (!successful) {
for (FileItem fileItem : items) {
try {
fileItem.delete();
} catch (Throwable e) {
// ignore it
}
}
}
}
}
#解析獲取到的fileItems
protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String encoding) {
MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<>();
Map<String, String[]> multipartParameters = new HashMap<>();
Map<String, String> multipartParameterContentTypes = new HashMap<>();
// Extract multipart files and multipart parameters.
for (FileItem fileItem : fileItems) {
// 是否是表單欄位(下面的解析可以看到構建時該欄位傳參 fileName == null),也就是文件名是否為空
if (fileItem.isFormField()) {
String value;
String partEncoding = determineEncoding(fileItem.getContentType(), encoding);
try {
value = fileItem.getString(partEncoding);
}
catch (UnsupportedEncodingException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Could not decode multipart item '" + fileItem.getFieldName() +
"' with encoding '" + partEncoding + "': using platform default");
}
value = fileItem.getString();
}
String[] curParam = multipartParameters.get(fileItem.getFieldName());
if (curParam == null) {
// simple form field
multipartParameters.put(fileItem.getFieldName(), new String[] {value});
}
else {
// array of simple form fields
String[] newParam = StringUtils.addStringToArray(curParam, value);
multipartParameters.put(fileItem.getFieldName(), newParam);
}
multipartParameterContentTypes.put(fileItem.getFieldName(), fileItem.getContentType());
}
else {
// multipart file field 構建MultipartFile
CommonsMultipartFile file = createMultipartFile(fileItem);
// 以文件欄位名為key (files)
multipartFiles.add(file.getName(), file);
if (logger.isDebugEnabled()) {
logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize() +
" bytes with original filename [" + file.getOriginalFilename() + "], stored " +
file.getStorageDescription());
}
}
}
return new MultipartParsingResult(multipartFiles, multipartParameters, multipartParameterContentTypes);
}
主要邏輯是這行程式碼FileItemIterator iter = getItemIterator(ctx);
,FileItem流迭代器的構造
#構造方法
FileItemIteratorImpl(RequestContext ctx)
throws FileUploadException, IOException {
if (ctx == null) {
throw new NullPointerException("ctx parameter");
}
// 獲取request的content-type,需要以multipart/ 開頭
String contentType = ctx.getContentType();
if ((null == contentType)
|| (!contentType.toLowerCase(Locale.ENGLISH).startsWith(MULTIPART))) {
throw new InvalidContentTypeException(
format("the request doesn't contain a %s or %s stream, content type header is %s",
MULTIPART_FORM_DATA, MULTIPART_MIXED, contentType));
}
// 獲取request的輸入流
InputStream input = ctx.getInputStream();
// 獲取內容長度 content-length 從request中取
@SuppressWarnings("deprecation") // still has to be backward compatible
final int contentLengthInt = ctx.getContentLength();
// 通過request.getHeader()取
final long requestSize = UploadContext.class.isAssignableFrom(ctx.getClass())
// Inline conditional is OK here CHECKSTYLE:OFF
? ((UploadContext) ctx).contentLength()
: contentLengthInt;
// CHECKSTYLE:ON
// sizeMax限制流大小 -1則不限制
if (sizeMax >= 0) {
if (requestSize != -1 && requestSize > sizeMax) {
throw new SizeLimitExceededException(
format("the request was rejected because its size (%s) exceeds the configured maximum (%s)",
Long.valueOf(requestSize), Long.valueOf(sizeMax)),
requestSize, sizeMax);
}
input = new LimitedInputStream(input, sizeMax) {
@Override
protected void raiseError(long pSizeMax, long pCount)
throws IOException {
FileUploadException ex = new SizeLimitExceededException(
format("the request was rejected because its size (%s) exceeds the configured maximum (%s)",
Long.valueOf(pCount), Long.valueOf(pSizeMax)),
pCount, pSizeMax);
throw new FileUploadIOException(ex);
}
};
}
// 獲取字元編碼
String charEncoding = headerEncoding;
if (charEncoding == null) {
charEncoding = ctx.getCharacterEncoding();
}
// 通過content-type = multipart/form-data; boundary=--------------------------205940049223747054037567
// 獲取boundary的值分隔符(一串隨機字元?)並轉化為位元組數組
boundary = getBoundary(contentType);
if (boundary == null) {
throw new FileUploadException("the request was rejected because no multipart boundary was found");
}
// 進度更新器
notifier = new MultipartStream.ProgressNotifier(listener, requestSize);
try {
// 構建多元流
multi = new MultipartStream(input, boundary, notifier);
} catch (IllegalArgumentException iae) {
throw new InvalidContentTypeException(
format("The boundary specified in the %s header is too long", CONTENT_TYPE), iae);
}
// 設置請求頭編碼
multi.setHeaderEncoding(charEncoding);
// 跳過序言
skipPreamble = true;
// 開始找第一個文件項目
findNextItem();
}
接著再來看下MultipartStream的構建
#MultipartStream構造
public MultipartStream(InputStream input, // request輸入流
byte[] boundary, // 邊界 位元組數組
int bufSize, // 緩衝區大小 默認4096
ProgressNotifier pNotifier) {
if (boundary == null) {
throw new IllegalArgumentException("boundary may not be null");
}
// We prepend CR/LF to the boundary to chop trailing CR/LF from
// body-data tokens. CR 回車\r LF 換行\n
// protected static final byte[] BOUNDARY_PREFIX = {CR, LF, DASH, DASH};
this.boundaryLength = boundary.length + BOUNDARY_PREFIX.length;
// 緩衝區大小判斷
if (bufSize < this.boundaryLength + 1) {
throw new IllegalArgumentException(
"The buffer size specified for the MultipartStream is too small");
}
this.input = input;
// 重新確定緩衝區大小
this.bufSize = Math.max(bufSize, boundaryLength * 2);
// 創建緩衝區 用來從讀inputStream 接受數據
this.buffer = new byte[this.bufSize];
this.notifier = pNotifier;
// 邊界數組
this.boundary = new byte[this.boundaryLength];
this.keepRegion = this.boundary.length;
// 將BOUNDARY_PREFIX數組和入參boundary數組的內容按序複製到新的boundary中
System.arraycopy(BOUNDARY_PREFIX, 0, this.boundary, 0,
BOUNDARY_PREFIX.length);
System.arraycopy(boundary, 0, this.boundary, BOUNDARY_PREFIX.length,
boundary.length);
// head和tail為緩衝區操作的索引
// 0 <= head < bufSize
// 0 <= tail <= bufSize
head = 0;
tail = 0;
}
接著看findNextItem方法,找第一個文件項目
/**
* Called for finding the next item, if any.
*
* @return True, if an next item was found, otherwise false.
* @throws IOException An I/O error occurred.
*/
private boolean findNextItem() throws IOException {
if (eof) {
return false;
}
// 開始為null
if (currentItem != null) {
currentItem.close();
currentItem = null;
}
for (;;) {
boolean nextPart;
if (skipPreamble) {
// 丟棄直到邊界分隔符的所有數據 再讀取邊界
nextPart = multi.skipPreamble();
} else {
// 直接讀取邊界
nextPart = multi.readBoundary();
}
if (!nextPart) {
if (currentFieldName == null) {
// Outer multipart terminated -> No more data
eof = true;
return false;
}
// Inner multipart terminated -> Return to parsing the outer
multi.setBoundary(boundary);
currentFieldName = null;
continue;
}
// 解析頭部 multi.readHeaders()從緩衝區解析到所有請求頭的字元串
// 接著getParsedHeaders將字元串按\r\n分隔,因為每一行數據都是一個請求頭內容
FileItemHeaders headers = getParsedHeaders(multi.readHeaders());
if (currentFieldName == null) {
// We're parsing the outer multipart
// 獲取上傳文件欄位參數名name = files
String fieldName = getFieldName(headers);
if (fieldName != null) {
String subContentType = headers.getHeader(CONTENT_TYPE); // img/jpeg
if (subContentType != null
&& subContentType.toLowerCase(Locale.ENGLISH)
.startsWith(MULTIPART_MIXED)) {
currentFieldName = fieldName;
// Multiple files associated with this field name
byte[] subBoundary = getBoundary(subContentType);
multi.setBoundary(subBoundary);
skipPreamble = true;
continue;
}
// 文件名 IMG_0908.JPG
String fileName = getFileName(headers);
// 根據欄位名、文件名、請求頭等構建FileItemStream對象
currentItem = new FileItemStreamImpl(fileName,
fieldName, headers.getHeader(CONTENT_TYPE),
fileName == null, getContentLength(headers));
// 設置請求頭
currentItem.setHeaders(headers);
// ++items
notifier.noteItem();
// 當期有可用item
itemValid = true;
return true;
}
} else {
String fileName = getFileName(headers);
if (fileName != null) {
currentItem = new FileItemStreamImpl(fileName,
currentFieldName,
headers.getHeader(CONTENT_TYPE),
false, getContentLength(headers));
currentItem.setHeaders(headers);
notifier.noteItem();
itemValid = true;
return true;
}
}
multi.discardBodyData();
}
}
下圖為緩衝區數據,首行為boundary分隔符內容也就是上文提到的—-加一串計算出來的隨機字元,\r\n後接著為content-Disposition和content-type請求頭,可以看到 HEADER_SEPARATOR頭部分隔符\r\n\r\n 和分隔符之前的為請求頭數據
另外boundary位元組數組對應的內容為————————–031262929361076583805179,下圖的首行內容比其多兩個-
下圖為解析完後的頭部
接下來再看下FileItemStreamImpl的構造過程,比較簡單
#FileItemStreamImpl構造方法
FileItemStreamImpl(String pName, String pFieldName,
String pContentType, boolean pFormField,
long pContentLength) throws IOException {
name = pName;
fieldName = pFieldName;
contentType = pContentType;
formField = pFormField;
// 創建itemStream流 本質上是從request的inputstream獲取數據
// 從head位置再開始找boundary邊界分隔符,若找到將邊界的前一個索引賦值給pos變數,並且當前文件可讀字元數為pos - head
final ItemInputStream itemStream = multi.newInputStream();
InputStream istream = itemStream;
// 若文件大小限制,超出長度會拋出異常
if (fileSizeMax != -1) {
if (pContentLength != -1
&& pContentLength > fileSizeMax) {
FileSizeLimitExceededException e =
new FileSizeLimitExceededException(
format("The field %s exceeds its maximum permitted size of %s bytes.",
fieldName, Long.valueOf(fileSizeMax)),
pContentLength, fileSizeMax);
e.setFileName(pName);
e.setFieldName(pFieldName);
throw new FileUploadIOException(e);
}
istream = new LimitedInputStream(istream, fileSizeMax) {
@Override
protected void raiseError(long pSizeMax, long pCount)
throws IOException {
itemStream.close(true);
FileSizeLimitExceededException e =
new FileSizeLimitExceededException(
format("The field %s exceeds its maximum permitted size of %s bytes.",
fieldName, Long.valueOf(pSizeMax)),
pCount, pSizeMax);
e.setFieldName(fieldName);
e.setFileName(name);
throw new FileUploadIOException(e);
}
};
}
stream = istream;
}
再來看看ItemInputStream,上面的FileItemStreamImpl對象有這個類型參數,主要用來獲取請求流的,因為ItemInputStream類是MultipartStream的內部類,能夠調用MultipartStream中的input流。
ItemInputStream
public class ItemInputStream extends InputStream implements Closeable {
// 目前已經讀取的位元組數
private long total;
// 必須保持的位元組數可能是分隔符boundary的一部分
private int pad;
// 緩衝區的當前偏移
private int pos;
// stream流是否關閉
private boolean closed;
// 構造方法
ItemInputStream() {
findSeparator();
}
// 尋找邊界分隔符boundary的前一個索引
private void findSeparator() {
pos = MultipartStream.this.findSeparator();
if (pos == -1) {
if (tail - head > keepRegion) {
pad = keepRegion;
} else {
pad = tail - head;
}
}
}
// 可讀取位元組數
@Override
public int available() throws IOException {
// 可讀=尾-首-邊界長度
if (pos == -1) {
return tail - head - pad;
}
// pos !=-1 說明pos後面是邊界了 只能讀到這個邊界之前的數據
// 可讀 = 邊界前的最後一個索引 - 首
return pos - head;
}
private static final int BYTE_POSITIVE_OFFSET = 256;
// 讀取stream流的下一個字元
@Override
public int read() throws IOException {
if (closed) {
throw new FileItemStream.ItemSkippedException();
}
if (available() == 0 && makeAvailable() == 0) {
return -1;
}
++total;
int b = buffer[head++];
if (b >= 0) {
return b;
}
// 如果負的 加上256
return b + BYTE_POSITIVE_OFFSET;
}
// 讀取位元組到給定的緩衝區b中
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (closed) {
throw new FileItemStream.ItemSkippedException();
}
if (len == 0) {
return 0;
}
int res = available();
if (res == 0) {
res = makeAvailable();
if (res == 0) {
return -1;
}
}
res = Math.min(res, len);
System.arraycopy(buffer, head, b, off, res);
// head加偏移
head += res;
total += res;
return res;
}
// 關閉輸入流
@Override
public void close() throws IOException {
close(false);
}
/**
* Closes the input stream.
*
* @param pCloseUnderlying Whether to close the underlying stream
* (hard close)
* @throws IOException An I/O error occurred.
*/
public void close(boolean pCloseUnderlying) throws IOException {
if (closed) {
return;
}
if (pCloseUnderlying) {
closed = true;
input.close();
} else {
for (;;) {
int av = available();
if (av == 0) {
av = makeAvailable();
if (av == 0) {
break;
}
}
skip(av);
}
}
closed = true;
}
// 跳過緩衝區中給定長度的位元組
@Override
public long skip(long bytes) throws IOException {
if (closed) {
throw new FileItemStream.ItemSkippedException();
}
int av = available();
if (av == 0) {
av = makeAvailable();
if (av == 0) {
return 0;
}
}
long res = Math.min(av, bytes);
head += res;
return res;
}
// 試圖讀取更多的數據,返回可讀位元組數
private int makeAvailable() throws IOException {
if (pos != -1) {
return 0;
}
// 將數據移到緩衝區的開頭,捨棄邊界
total += tail - head - pad;
System.arraycopy(buffer, tail - pad, buffer, 0, pad);
// Refill buffer with new data.
head = 0;
tail = pad;
for (;;) {
// 讀取tail位置開始讀 bufSize-tail長度的位元組到buffer緩衝區中
int bytesRead = input.read(buffer, tail, bufSize - tail);
if (bytesRead == -1) {
// The last pad amount is left in the buffer.
// Boundary can't be in there so signal an error
// condition.
final String msg = "Stream ended unexpectedly";
throw new MalformedStreamException(msg);
}
if (notifier != null) {
notifier.noteBytesRead(bytesRead);
}
// tail加偏移
tail += bytesRead;
// 再嘗試找boundary邊界,賦值pos -1
findSeparator();
int av = available();
// 返回可讀位元組數
if (av > 0 || pos != -1) {
return av;
}
}
}
// 判斷流是否關閉
public boolean isClosed() {
return closed;
}
}
#接受請求
請求解析完成後就可以以文件對象接收了,參數類型為MultipartFile,可強轉為CommonsMultipartFile,參數名需要與上傳文件的fieldName相對應或者也可以用@RequestParam註解指定參數名
@RequestMapping(value = "file/upload", method = RequestMethod.POST)
@ResponseBody
public Object uploadFile(MultipartFile[] files, HttpServletRequest request, HttpServletResponse response) throws IOException {
....... 上傳邏輯
return CommonResult.succ("上傳成功");
}
可以用postman測試,content-type的boundary顯示是請求發送時計算,不知道怎麼算的反正是一串隨機數,是用來分隔多個文件內容的,在源碼中可以看到不能缺失否則解析過程中會報錯