|
|
@@ -37,11 +37,6 @@ import com.jfinal.render.JsonRender;
|
|
|
import com.jfinal.render.Render;
|
|
|
import com.jfinal.render.RenderManager;
|
|
|
import com.jfinal.upload.*;
|
|
|
-import org.apache.commons.fileupload.FileItem;
|
|
|
-import org.apache.commons.fileupload.FileItemFactory;
|
|
|
-import org.apache.commons.fileupload.ProgressListener;
|
|
|
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
|
|
|
|
/**
|
|
|
* Controller
|
|
|
@@ -1512,52 +1507,7 @@ public abstract class Controller {
|
|
|
* @return UploadFile 对象
|
|
|
*/
|
|
|
public UploadFile getProgressFile(String parameterName, String uploadPath, Consumer<UploadProgress> callback) {
|
|
|
- HttpServletRequest request = getRequest();
|
|
|
- // 检查请求是否包含文件上传
|
|
|
- if (!ServletFileUpload.isMultipartContent(request)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- UploadFile progressFile = null;
|
|
|
- // 创建文件项工厂
|
|
|
- FileItemFactory factory = new DiskFileItemFactory();
|
|
|
- // 创建上传处理器
|
|
|
- ServletFileUpload upload = new ServletFileUpload(factory);
|
|
|
- // 创建进度监听器
|
|
|
- ProgressListener progressListener = new ProgressListener() {
|
|
|
- @Override
|
|
|
- public void update(long bytesRead, long contentLength, int items) {
|
|
|
- callback.accept(new UploadProgress(items, contentLength, bytesRead));
|
|
|
- }
|
|
|
- };
|
|
|
- // 将进度监听器添加到上传处理器
|
|
|
- upload.setProgressListener(progressListener);
|
|
|
- try {
|
|
|
- List<FileItem> formItems = upload.parseRequest(request);
|
|
|
- if (formItems != null && !formItems.isEmpty()) {
|
|
|
- FileItem fileItem = null;
|
|
|
- if (StrKit.isBlank(parameterName)) {
|
|
|
- fileItem = formItems.stream().filter(item -> !item.isFormField()).findFirst().orElse(null);
|
|
|
- } else {
|
|
|
- fileItem = formItems.stream().filter(item -> (!item.isFormField() && parameterName.equals(item.getFieldName()))).findFirst().orElse(null);
|
|
|
- }
|
|
|
- if (fileItem != null) {
|
|
|
- // 处理上传的文件
|
|
|
- String originFileName = fileItem.getName();
|
|
|
- String finalUploadPath = JFinal.me().getConstants().getBaseUploadPath() + (StrKit.isBlank(uploadPath) ? "" : (File.separator + uploadPath));
|
|
|
- String newFileName = ProgressUploadFileConfig.getRenameFunc().call(finalUploadPath, originFileName);
|
|
|
- String filePath = finalUploadPath + File.separator + newFileName;
|
|
|
- File storeFile = new File(filePath);
|
|
|
- // 保存文件到硬盘
|
|
|
- fileItem.write(storeFile);
|
|
|
- progressFile = new UploadFile(parameterName, finalUploadPath, storeFile.getName(), originFileName, fileItem.getContentType());
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- return progressFile;
|
|
|
+ return ProgressUploadFileKit.get(getRequest(), parameterName, uploadPath, callback);
|
|
|
}
|
|
|
}
|
|
|
|