mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
prepare 5.3.0
This commit is contained in:
@@ -11,14 +11,14 @@ import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.core.net.multipart.MultipartFormData;
|
||||
import cn.hutool.core.net.multipart.UploadSetting;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.extra.servlet.multipart.MultipartFormData;
|
||||
import cn.hutool.extra.servlet.multipart.UploadSetting;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.ServletRequest;
|
||||
@@ -265,7 +265,7 @@ public class ServletUtil {
|
||||
public static MultipartFormData getMultipart(ServletRequest request, UploadSetting uploadSetting) throws IORuntimeException {
|
||||
final MultipartFormData formData = new MultipartFormData(uploadSetting);
|
||||
try {
|
||||
formData.parseRequest(request);
|
||||
formData.parseRequestStream(request.getInputStream(), request.getCharacterEncoding());
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
|
@@ -1,257 +0,0 @@
|
||||
package cn.hutool.extra.servlet.multipart;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
|
||||
/**
|
||||
* HttpRequest解析器<br>
|
||||
* 来自Jodd
|
||||
*
|
||||
* @author jodd.org
|
||||
*/
|
||||
public class MultipartFormData {
|
||||
|
||||
/** 请求参数 */
|
||||
private Map<String, String[]> requestParameters = new HashMap<String, String[]>();
|
||||
/** 请求文件 */
|
||||
private Map<String, UploadFile[]> requestFiles = new HashMap<String, UploadFile[]>();
|
||||
|
||||
/** 是否解析完毕 */
|
||||
private boolean loaded;
|
||||
/** 上传选项 */
|
||||
private UploadSetting setting;
|
||||
|
||||
// --------------------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public MultipartFormData() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param uploadSetting 上传设定
|
||||
*/
|
||||
public MultipartFormData(UploadSetting uploadSetting) {
|
||||
this.setting = uploadSetting == null ? new UploadSetting() : uploadSetting;
|
||||
}
|
||||
// --------------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 解析上传文件和表单数据
|
||||
*
|
||||
* @param request Http请求
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
public void parseRequest(ServletRequest request) throws IOException {
|
||||
parseRequestStream(request.getInputStream(), request.getCharacterEncoding());
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取上传的文件和表单数据
|
||||
*
|
||||
* @param inputStream HttpRequest流
|
||||
* @param charset 编码
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
public void parseRequestStream(InputStream inputStream, String charset) throws IOException {
|
||||
setLoaded();
|
||||
|
||||
MultipartRequestInputStream input = new MultipartRequestInputStream(inputStream);
|
||||
input.readBoundary();
|
||||
while (true) {
|
||||
UploadFileHeader header = input.readDataHeader(charset);
|
||||
if (header == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (header.isFile == true) {
|
||||
// 文件类型的表单项
|
||||
String fileName = header.fileName;
|
||||
if (fileName.length() > 0 && header.contentType.contains("application/x-macbinary")) {
|
||||
input.skipBytes(128);
|
||||
}
|
||||
UploadFile newFile = new UploadFile(header, setting);
|
||||
newFile.processStream(input);
|
||||
|
||||
putFile(header.formFieldName, newFile);
|
||||
} else {
|
||||
// 标准表单项
|
||||
ByteArrayOutputStream fbos = new ByteArrayOutputStream(1024);
|
||||
input.copy(fbos);
|
||||
String value = (charset != null) ? new String(fbos.toByteArray(), charset) : new String(fbos.toByteArray());
|
||||
putParameter(header.formFieldName, value);
|
||||
}
|
||||
|
||||
input.skipBytes(1);
|
||||
input.mark(1);
|
||||
|
||||
// read byte, but may be end of stream
|
||||
int nextByte = input.read();
|
||||
if (nextByte == -1 || nextByte == '-') {
|
||||
input.reset();
|
||||
break;
|
||||
}
|
||||
input.reset();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- parameters
|
||||
/**
|
||||
* 返回单一参数值,如果有多个只返回第一个
|
||||
*
|
||||
* @param paramName 参数名
|
||||
* @return null未找到,否则返回值
|
||||
*/
|
||||
public String getParam(String paramName) {
|
||||
if (requestParameters == null) {
|
||||
return null;
|
||||
}
|
||||
String[] values = requestParameters.get(paramName);
|
||||
if (ArrayUtil.isNotEmpty(values)) {
|
||||
return values[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获得参数名集合
|
||||
*/
|
||||
public Set<String> getParamNames() {
|
||||
if (requestParameters == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return requestParameters.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得数组表单值
|
||||
*
|
||||
* @param paramName 参数名
|
||||
* @return 数组表单值
|
||||
*/
|
||||
public String[] getArrayParam(String paramName) {
|
||||
if (requestParameters == null) {
|
||||
return null;
|
||||
}
|
||||
return requestParameters.get(paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有属性的集合
|
||||
*
|
||||
* @return 所有属性的集合
|
||||
*/
|
||||
public Map<String, String[]> getParamMap() {
|
||||
return requestParameters;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------- Files parameters
|
||||
/**
|
||||
* 获取上传的文件
|
||||
*
|
||||
* @param paramName 文件参数名称
|
||||
* @return 上传的文件, 如果无为null
|
||||
*/
|
||||
public UploadFile getFile(String paramName) {
|
||||
UploadFile[] values = getFiles(paramName);
|
||||
if ((values != null) && (values.length > 0)) {
|
||||
return values[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得某个属性名的所有文件<br>
|
||||
* 当表单中两个文件使用同一个name的时候
|
||||
*
|
||||
* @param paramName 属性名
|
||||
* @return 上传的文件列表
|
||||
*/
|
||||
public UploadFile[] getFiles(String paramName) {
|
||||
if (requestFiles == null) {
|
||||
return null;
|
||||
}
|
||||
return requestFiles.get(paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传的文件属性名集合
|
||||
*
|
||||
* @return 上传的文件属性名集合
|
||||
*/
|
||||
public Set<String> getFileParamNames() {
|
||||
if (requestFiles == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return requestFiles.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件映射
|
||||
*
|
||||
* @return 文件映射
|
||||
*/
|
||||
public Map<String, UploadFile[]> getFileMap() {
|
||||
return this.requestFiles;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------- Load
|
||||
/**
|
||||
* 是否已被解析
|
||||
*
|
||||
* @return 如果流已被解析返回true
|
||||
*/
|
||||
public boolean isLoaded() {
|
||||
return loaded;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* 加入上传文件
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param uploadFile 文件
|
||||
*/
|
||||
private void putFile(String name, UploadFile uploadFile) {
|
||||
UploadFile[] fileUploads = requestFiles.get(name);
|
||||
fileUploads = fileUploads == null ? new UploadFile[] { uploadFile } : ArrayUtil.append(fileUploads, uploadFile);
|
||||
requestFiles.put(name, fileUploads);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入普通参数
|
||||
*
|
||||
* @param name 参数名
|
||||
* @param value 参数值
|
||||
*/
|
||||
private void putParameter(String name, String value) {
|
||||
String[] params = requestParameters.get(name);
|
||||
params = params == null ? new String[] { value } : ArrayUtil.append(params, value);
|
||||
requestParameters.put(name, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置使输入流为解析状态,如果已解析,则抛出异常
|
||||
*
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
private void setLoaded() throws IOException {
|
||||
if (loaded == true) {
|
||||
throw new IOException("Multi-part request already parsed.");
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
// ---------------------------------------------------------------- Private method end
|
||||
}
|
@@ -1,216 +0,0 @@
|
||||
package cn.hutool.extra.servlet.multipart;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Http请求解析流,提供了专门针对带文件的form表单的解析<br>
|
||||
* 来自Jodd
|
||||
*
|
||||
* @author jodd.org
|
||||
*/
|
||||
public class MultipartRequestInputStream extends BufferedInputStream {
|
||||
|
||||
public MultipartRequestInputStream(InputStream in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取byte字节流,在末尾抛出异常
|
||||
*
|
||||
* @return byte
|
||||
* @throws IOException 读取异常
|
||||
*/
|
||||
public byte readByte() throws IOException {
|
||||
int i = super.read();
|
||||
if (i == -1) {
|
||||
throw new IOException("End of HTTP request stream reached");
|
||||
}
|
||||
return (byte) i;
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳过指定位数的 bytes.
|
||||
*
|
||||
* @param i 跳过的byte数
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
public void skipBytes(int i) throws IOException {
|
||||
long len = super.skip(i);
|
||||
if (len != i) {
|
||||
throw new IOException("Unable to skip data in HTTP request");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- boundary
|
||||
|
||||
/** part部分边界 */
|
||||
protected byte[] boundary;
|
||||
|
||||
/**
|
||||
* 输入流中读取边界
|
||||
*
|
||||
* @return 边界
|
||||
* @throws IOException 读取异常
|
||||
*/
|
||||
public byte[] readBoundary() throws IOException {
|
||||
ByteArrayOutputStream boundaryOutput = new ByteArrayOutputStream(1024);
|
||||
byte b;
|
||||
// skip optional whitespaces
|
||||
while ((b = readByte()) <= ' ') {
|
||||
}
|
||||
boundaryOutput.write(b);
|
||||
|
||||
// now read boundary chars
|
||||
while ((b = readByte()) != '\r') {
|
||||
boundaryOutput.write(b);
|
||||
}
|
||||
if (boundaryOutput.size() == 0) {
|
||||
throw new IOException("Problems with parsing request: invalid boundary");
|
||||
}
|
||||
skipBytes(1);
|
||||
boundary = new byte[boundaryOutput.size() + 2];
|
||||
System.arraycopy(boundaryOutput.toByteArray(), 0, boundary, 2, boundary.length - 2);
|
||||
boundary[0] = '\r';
|
||||
boundary[1] = '\n';
|
||||
return boundary;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- data header
|
||||
|
||||
protected UploadFileHeader lastHeader;
|
||||
|
||||
public UploadFileHeader getLastHeader() {
|
||||
return lastHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从流中读取文件头部信息, 如果达到末尾则返回null
|
||||
*
|
||||
* @param encoding 字符集
|
||||
* @return 头部信息, 如果达到末尾则返回null
|
||||
* @throws IOException 读取异常
|
||||
*/
|
||||
public UploadFileHeader readDataHeader(String encoding) throws IOException {
|
||||
String dataHeader = readDataHeaderString(encoding);
|
||||
if (dataHeader != null) {
|
||||
lastHeader = new UploadFileHeader(dataHeader);
|
||||
} else {
|
||||
lastHeader = null;
|
||||
}
|
||||
return lastHeader;
|
||||
}
|
||||
|
||||
protected String readDataHeaderString(String encoding) throws IOException {
|
||||
ByteArrayOutputStream data = new ByteArrayOutputStream();
|
||||
byte b;
|
||||
while (true) {
|
||||
// end marker byte on offset +0 and +2 must be 13
|
||||
if ((b = readByte()) != '\r') {
|
||||
data.write(b);
|
||||
continue;
|
||||
}
|
||||
mark(4);
|
||||
skipBytes(1);
|
||||
int i = read();
|
||||
if (i == -1) {
|
||||
// reached end of stream
|
||||
return null;
|
||||
}
|
||||
if (i == '\r') {
|
||||
reset();
|
||||
break;
|
||||
}
|
||||
reset();
|
||||
data.write(b);
|
||||
}
|
||||
skipBytes(3);
|
||||
return encoding == null ? data.toString() : data.toString(encoding);
|
||||
}
|
||||
// ---------------------------------------------------------------- copy
|
||||
|
||||
/**
|
||||
* 全部字节流复制到out
|
||||
*
|
||||
* @param out 输出流
|
||||
* @return 复制的字节数
|
||||
* @throws IOException 读取异常
|
||||
*/
|
||||
public int copy(OutputStream out) throws IOException {
|
||||
int count = 0;
|
||||
while (true) {
|
||||
byte b = readByte();
|
||||
if (isBoundary(b)) {
|
||||
break;
|
||||
}
|
||||
out.write(b);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制字节流到out, 大于maxBytes或者文件末尾停止
|
||||
*
|
||||
* @param out 输出流
|
||||
* @param limit 最大字节数
|
||||
* @return 复制的字节数
|
||||
* @throws IOException 读取异常
|
||||
*/
|
||||
public int copy(OutputStream out, int limit) throws IOException {
|
||||
int count = 0;
|
||||
while (true) {
|
||||
byte b = readByte();
|
||||
if (isBoundary(b)) {
|
||||
break;
|
||||
}
|
||||
out.write(b);
|
||||
count++;
|
||||
if (count > limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳过边界表示
|
||||
*
|
||||
* @return 跳过的字节数
|
||||
* @throws IOException 读取异常
|
||||
*/
|
||||
public int skipToBoundary() throws IOException {
|
||||
int count = 0;
|
||||
while (true) {
|
||||
byte b = readByte();
|
||||
count++;
|
||||
if (isBoundary(b)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param b byte
|
||||
* @return 是否为边界的标志
|
||||
* @throws IOException 读取异常
|
||||
*/
|
||||
public boolean isBoundary(byte b) throws IOException {
|
||||
int boundaryLen = boundary.length;
|
||||
mark(boundaryLen + 1);
|
||||
int bpos = 0;
|
||||
while (b == boundary[bpos]) {
|
||||
b = readByte();
|
||||
bpos++;
|
||||
if (bpos == boundaryLen) {
|
||||
return true; // boundary found!
|
||||
}
|
||||
}
|
||||
reset();
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -1,271 +0,0 @@
|
||||
package cn.hutool.extra.servlet.multipart;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
|
||||
/**
|
||||
* 上传的文件对象
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*
|
||||
*/
|
||||
public class UploadFile {
|
||||
private static Log log = LogFactory.get();
|
||||
|
||||
private static final String TMP_FILE_PREFIX = "hutool-";
|
||||
private static final String TMP_FILE_SUFFIX = ".upload.tmp";
|
||||
|
||||
private UploadFileHeader header;
|
||||
private UploadSetting setting;
|
||||
|
||||
private int size = -1;
|
||||
|
||||
// 文件流(小文件位于内存中)
|
||||
private byte[] data;
|
||||
// 临时文件(大文件位于临时文件夹中)
|
||||
private File tempFile;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param header 头部信息
|
||||
* @param setting 上传设置
|
||||
*/
|
||||
public UploadFile(UploadFileHeader header, UploadSetting setting) {
|
||||
this.header = header;
|
||||
this.setting = setting;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- operations
|
||||
|
||||
/**
|
||||
* 从磁盘或者内存中删除这个文件
|
||||
*/
|
||||
public void delete() {
|
||||
if (tempFile != null) {
|
||||
tempFile.delete();
|
||||
}
|
||||
if (data != null) {
|
||||
data = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将上传的文件写入指定的目标文件路径,自动创建文件<br>
|
||||
* 写入后原临时文件会被删除
|
||||
* @param destPath 目标文件路径
|
||||
* @return 目标文件
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
public File write(String destPath) throws IOException {
|
||||
if(data != null || tempFile != null) {
|
||||
return write(FileUtil.touch(destPath));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将上传的文件写入目标文件<br>
|
||||
* 写入后原临时文件会被删除
|
||||
*
|
||||
* @param destination 目标文件
|
||||
* @return 目标文件
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
public File write(File destination) throws IOException {
|
||||
assertValid();
|
||||
|
||||
if (destination.isDirectory() == true) {
|
||||
destination = new File(destination, this.header.getFileName());
|
||||
}
|
||||
if (data != null) {
|
||||
FileUtil.writeBytes(data, destination);
|
||||
data = null;
|
||||
} else {
|
||||
if (tempFile != null) {
|
||||
FileUtil.move(tempFile, destination, true);
|
||||
}
|
||||
}
|
||||
return destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获得文件字节流
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
public byte[] getFileContent() throws IOException {
|
||||
assertValid();
|
||||
|
||||
if (data != null) {
|
||||
return data;
|
||||
}
|
||||
if (tempFile != null) {
|
||||
return FileUtil.readBytes(tempFile);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获得文件流
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
public InputStream getFileInputStream() throws IOException {
|
||||
assertValid();
|
||||
|
||||
if (data != null) {
|
||||
return new BufferedInputStream(new ByteArrayInputStream(data));
|
||||
}
|
||||
if (tempFile != null) {
|
||||
return new BufferedInputStream(new FileInputStream(tempFile));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- header
|
||||
|
||||
/**
|
||||
* @return 上传文件头部信息
|
||||
*/
|
||||
public UploadFileHeader getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 文件名
|
||||
*/
|
||||
public String getFileName() {
|
||||
return header == null ? null : header.getFileName();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- properties
|
||||
|
||||
/**
|
||||
* @return 上传文件的大小,> 0 表示未上传
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否上传成功
|
||||
*/
|
||||
public boolean isUploaded() {
|
||||
return size > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 文件是否在内存中
|
||||
*/
|
||||
public boolean isInMemory() {
|
||||
return data != null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- process
|
||||
/**
|
||||
* 处理上传表单流,提取出文件
|
||||
*
|
||||
* @param input 上传表单的流
|
||||
* @return 是否成功
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
protected boolean processStream(MultipartRequestInputStream input) throws IOException {
|
||||
if (!isAllowedExtension()) {
|
||||
// 非允许的扩展名
|
||||
log.debug("Forbidden uploaded file [{}]", this.getFileName());
|
||||
size = input.skipToBoundary();
|
||||
return false;
|
||||
}
|
||||
size = 0;
|
||||
|
||||
// 处理内存文件
|
||||
int memoryThreshold = setting.memoryThreshold;
|
||||
if (memoryThreshold > 0) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(memoryThreshold);
|
||||
int written = input.copy(baos, memoryThreshold);
|
||||
data = baos.toByteArray();
|
||||
if (written <= memoryThreshold) {
|
||||
// 文件存放于内存
|
||||
size = data.length;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理硬盘文件
|
||||
tempFile = FileUtil.createTempFile(TMP_FILE_PREFIX, TMP_FILE_SUFFIX, FileUtil.touch(setting.tmpUploadPath), false);
|
||||
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
|
||||
if (data != null) {
|
||||
size = data.length;
|
||||
out.write(data);
|
||||
data = null; // not needed anymore
|
||||
}
|
||||
int maxFileSize = setting.maxFileSize;
|
||||
try {
|
||||
if (maxFileSize == -1) {
|
||||
size += input.copy(out);
|
||||
return true;
|
||||
}
|
||||
size += input.copy(out, maxFileSize - size + 1); // one more byte to detect larger files
|
||||
if (size > maxFileSize) {
|
||||
// 超出上传大小限制
|
||||
tempFile.delete();
|
||||
tempFile = null;
|
||||
log.debug("Upload file [{}] too big, file size > [{}]", this.getFileName(), maxFileSize);
|
||||
input.skipToBoundary();
|
||||
return false;
|
||||
}
|
||||
} finally {
|
||||
IoUtil.close(out);
|
||||
}
|
||||
// if (getFileName().length() == 0 && size == 0) {
|
||||
// size = -1;
|
||||
// }
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* @return 是否为允许的扩展名
|
||||
*/
|
||||
private boolean isAllowedExtension() {
|
||||
String[] exts = setting.fileExts;
|
||||
boolean isAllow = setting.isAllowFileExts;
|
||||
if (exts == null || exts.length == 0) {
|
||||
// 如果给定扩展名列表为空,当允许扩展名时全部允许,否则全部禁止
|
||||
return isAllow;
|
||||
}
|
||||
|
||||
String fileNameExt = FileUtil.extName(this.getFileName());
|
||||
for (String fileExtension : setting.fileExts) {
|
||||
if (fileNameExt.equalsIgnoreCase(fileExtension)) {
|
||||
return isAllow;
|
||||
}
|
||||
}
|
||||
|
||||
// 未匹配到扩展名,如果为允许列表,返回false, 否则true
|
||||
return !isAllow;
|
||||
}
|
||||
|
||||
/**
|
||||
* 断言是否文件流可用
|
||||
* @throws IOException IO异常
|
||||
*/
|
||||
private void assertValid() throws IOException {
|
||||
if(! isUploaded()) {
|
||||
throw new IOException(StrUtil.format("File [{}] upload fail", getFileName()));
|
||||
}
|
||||
}
|
||||
// ---------------------------------------------------------------------------- Private method end
|
||||
}
|
@@ -1,203 +0,0 @@
|
||||
package cn.hutool.extra.servlet.multipart;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 上传的文件的头部信息<br>
|
||||
* 来自Jodd
|
||||
*
|
||||
* @author jodd.org
|
||||
*/
|
||||
public class UploadFileHeader {
|
||||
|
||||
// String dataHeader;
|
||||
String formFieldName;
|
||||
|
||||
String formFileName;
|
||||
String path;
|
||||
String fileName;
|
||||
|
||||
boolean isFile;
|
||||
String contentType;
|
||||
String mimeType;
|
||||
String mimeSubtype;
|
||||
String contentDisposition;
|
||||
|
||||
UploadFileHeader(String dataHeader) {
|
||||
processHeaderString(dataHeader);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- public interface
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if uploaded data are correctly marked as a file. This is true if header contains string 'filename'.
|
||||
*
|
||||
* @return 是否为文件
|
||||
*/
|
||||
public boolean isFile() {
|
||||
return isFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回表单字段名
|
||||
*
|
||||
* @return 表单字段名
|
||||
*/
|
||||
public String getFormFieldName() {
|
||||
return formFieldName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回表单中的文件名,来自客户端传入
|
||||
*
|
||||
* @return 表单文件名
|
||||
*/
|
||||
public String getFormFileName() {
|
||||
return formFileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件名,不包括路径
|
||||
*
|
||||
* @return 文件名
|
||||
*/
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns uploaded content type. It is usually in the following form:<br>
|
||||
* mime_type/mime_subtype.
|
||||
*
|
||||
* @return content type
|
||||
* @see #getMimeType()
|
||||
* @see #getMimeSubtype()
|
||||
*/
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns file types MIME.
|
||||
*
|
||||
* @return types MIME
|
||||
*/
|
||||
public String getMimeType() {
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns file sub type MIME.
|
||||
*
|
||||
* @return sub type MIME
|
||||
*/
|
||||
public String getMimeSubtype() {
|
||||
return mimeSubtype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns content disposition. Usually it is 'form-data'.
|
||||
*
|
||||
* @return content disposition
|
||||
*/
|
||||
public String getContentDisposition() {
|
||||
return contentDisposition;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Private Method
|
||||
|
||||
/**
|
||||
* 获得头信息字符串字符串中指定的值
|
||||
*
|
||||
* @param dataHeader 头信息
|
||||
* @param fieldName 字段名
|
||||
* @return 字段值
|
||||
*/
|
||||
private String getDataFieldValue(String dataHeader, String fieldName) {
|
||||
String value = null;
|
||||
String token = StrUtil.format("{}=\"", fieldName);
|
||||
int pos = dataHeader.indexOf(token);
|
||||
if (pos > 0) {
|
||||
int start = pos + token.length();
|
||||
int end = dataHeader.indexOf('"', start);
|
||||
if ((start > 0) && (end > 0)) {
|
||||
value = dataHeader.substring(start, end);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 头信息中获得content type
|
||||
*
|
||||
* @param dataHeader data header string
|
||||
* @return content type or an empty string if no content type defined
|
||||
*/
|
||||
private String getContentType(String dataHeader) {
|
||||
String token = "Content-Type:";
|
||||
int start = dataHeader.indexOf(token);
|
||||
if (start == -1) {
|
||||
return StrUtil.EMPTY;
|
||||
}
|
||||
start += token.length();
|
||||
return dataHeader.substring(start);
|
||||
}
|
||||
|
||||
private String getContentDisposition(String dataHeader) {
|
||||
int start = dataHeader.indexOf(':') + 1;
|
||||
int end = dataHeader.indexOf(';');
|
||||
return dataHeader.substring(start, end);
|
||||
}
|
||||
|
||||
private String getMimeType(String ContentType) {
|
||||
int pos = ContentType.indexOf('/');
|
||||
if (pos == -1) {
|
||||
return ContentType;
|
||||
}
|
||||
return ContentType.substring(1, pos);
|
||||
}
|
||||
|
||||
private String getMimeSubtype(String ContentType) {
|
||||
int start = ContentType.indexOf('/');
|
||||
if (start == -1) {
|
||||
return ContentType;
|
||||
}
|
||||
start++;
|
||||
return ContentType.substring(start);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理头字符串,使之转化为字段
|
||||
*
|
||||
* @param dataHeader 头字符串
|
||||
*/
|
||||
private void processHeaderString(String dataHeader) {
|
||||
isFile = dataHeader.indexOf("filename") > 0;
|
||||
formFieldName = getDataFieldValue(dataHeader, "name");
|
||||
if (isFile) {
|
||||
formFileName = getDataFieldValue(dataHeader, "filename");
|
||||
if (formFileName == null) {
|
||||
return;
|
||||
}
|
||||
if (formFileName.length() == 0) {
|
||||
path = StrUtil.EMPTY;
|
||||
fileName = StrUtil.EMPTY;
|
||||
}
|
||||
int ls = FileUtil.lastIndexOfSeparator(formFileName);
|
||||
if (ls == -1) {
|
||||
path = StrUtil.EMPTY;
|
||||
fileName = formFileName;
|
||||
} else {
|
||||
path = formFileName.substring(0, ls);
|
||||
fileName = formFileName.substring(ls);
|
||||
}
|
||||
if (fileName.length() > 0) {
|
||||
this.contentType = getContentType(dataHeader);
|
||||
mimeType = getMimeType(contentType);
|
||||
mimeSubtype = getMimeSubtype(contentType);
|
||||
contentDisposition = getContentDisposition(dataHeader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,148 +0,0 @@
|
||||
package cn.hutool.extra.servlet.multipart;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.setting.Setting;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* 上传文件设定文件
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*
|
||||
*/
|
||||
public class UploadSetting {
|
||||
private static final Log log = Log.get();
|
||||
|
||||
/** 默认的配置文件路径(相对ClassPath) */
|
||||
public final static String DEFAULT_SETTING_PATH = "config/upload.setting";
|
||||
|
||||
/** 最大文件大小,默认无限制 */
|
||||
protected int maxFileSize = -1;
|
||||
/** 文件保存到内存的边界 */
|
||||
protected int memoryThreshold = 8192;
|
||||
/** 临时文件目录 */
|
||||
protected String tmpUploadPath;
|
||||
/** 文件扩展名限定 */
|
||||
protected String[] fileExts;
|
||||
/** 扩展名是允许列表还是禁止列表 */
|
||||
protected boolean isAllowFileExts = true;
|
||||
|
||||
public UploadSetting() {
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------- Setters and Getters start
|
||||
/**
|
||||
* @return 获得最大文件大小,-1表示无限制
|
||||
*/
|
||||
public int getMaxFileSize() {
|
||||
return maxFileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设定最大文件大小,-1表示无限制
|
||||
*
|
||||
* @param maxFileSize 最大文件大小
|
||||
*/
|
||||
public void setMaxFileSize(int maxFileSize) {
|
||||
this.maxFileSize = maxFileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 文件保存到内存的边界
|
||||
*/
|
||||
public int getMemoryThreshold() {
|
||||
return memoryThreshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设定文件保存到内存的边界<br>
|
||||
* 如果文件大小小于这个边界,将保存于内存中,否则保存至临时目录中
|
||||
*
|
||||
* @param memoryThreshold 文件保存到内存的边界
|
||||
*/
|
||||
public void setMemoryThreshold(int memoryThreshold) {
|
||||
this.memoryThreshold = memoryThreshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 上传文件的临时目录,若为空,使用系统目录
|
||||
*/
|
||||
public String getTmpUploadPath() {
|
||||
return tmpUploadPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设定上传文件的临时目录,null表示使用系统临时目录
|
||||
*
|
||||
* @param tmpUploadPath 临时目录,绝对路径
|
||||
*/
|
||||
public void setTmpUploadPath(String tmpUploadPath) {
|
||||
this.tmpUploadPath = tmpUploadPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 文件扩展名限定列表
|
||||
*/
|
||||
public String[] getFileExts() {
|
||||
return fileExts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设定文件扩展名限定里列表<br>
|
||||
* 禁止列表还是允许列表取决于isAllowFileExts
|
||||
*
|
||||
* @param fileExts 文件扩展名列表
|
||||
*/
|
||||
public void setFileExts(String[] fileExts) {
|
||||
this.fileExts = fileExts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否允许文件扩展名<br>
|
||||
*
|
||||
* @return 若true表示只允许列表里的扩展名,否则是禁止列表里的扩展名
|
||||
*/
|
||||
public boolean isAllowFileExts() {
|
||||
return isAllowFileExts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设定是否允许扩展名
|
||||
*
|
||||
* @param isAllowFileExts 若true表示只允许列表里的扩展名,否则是禁止列表里的扩展名
|
||||
*/
|
||||
public void setAllowFileExts(boolean isAllowFileExts) {
|
||||
this.isAllowFileExts = isAllowFileExts;
|
||||
}
|
||||
// ---------------------------------------------------------------------- Setters and Getters end
|
||||
|
||||
/**
|
||||
* 加载全局设定<br>
|
||||
* 使用默认的配置文件classpath/config/upload.setting
|
||||
*/
|
||||
public void load() {
|
||||
load(DEFAULT_SETTING_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载全局设定
|
||||
*
|
||||
* @param settingPath 设定文件路径,相对Classpath
|
||||
*/
|
||||
synchronized public void load(String settingPath) {
|
||||
URL url = URLUtil.getURL(settingPath);
|
||||
if (url == null) {
|
||||
log.info("Can not find Upload setting file [{}], use default setting.", settingPath);
|
||||
return;
|
||||
}
|
||||
Setting setting = new Setting(url, Setting.DEFAULT_CHARSET, true);
|
||||
|
||||
maxFileSize = setting.getInt("file.size.max");
|
||||
memoryThreshold = setting.getInt("memory.threshold");
|
||||
tmpUploadPath = setting.getStr("tmp.upload.path");
|
||||
fileExts = setting.getStrings("file.exts");
|
||||
isAllowFileExts = setting.getBool("file.exts.allow");
|
||||
}
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
/**
|
||||
* 基于Servlet的文件上传封装
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.servlet.multipart;
|
Reference in New Issue
Block a user