我有一个奇怪的情况。
我需要管理一个进度递增的上传文件,该文件需要与Ancient.ie版本9配合使用。
上载只能通过提交iframe
POST来完成,我们利用Flow js来完成。在上载期间,将发出Ajax期间请求以监视上载进度并在UI上填写指标。
@Scope("request")
@PreAuthorize("hasFunction('DMS')")
@Controller("/secure/admin/pages/dms/fileRepository")
@RequestMapping("/secure/admin/pages/dms/fileRepository")
public class FileRepositoryController extends BaseController
{
private final static Logger log = LogManager.getLogger();
@Autowired
private FileRepositoryManager fileRepositoryManager;
@Autowired
private ApplicationEventMulticaster eventDispatcher;
@RequestMapping(value = "/uploadProgress",
method =
{
RequestMethod.POST,
RequestMethod.GET
})
public PhoenixResponse uploadProgress(HttpSession session)
{
UploadInfo info = (UploadInfo) session.getAttribute(UploadInfo.SESSION_KEY);
if (info == null)
{
log.warn("Upload info not found in session");
return info().withSingleton(new UploadInfo());
}
PhoenixResponse res;
switch (info.getStatus())
{
case DONE:
{
session.removeAttribute(UploadInfo.SESSION_KEY);
res = success().withAlert("Upload OK", DEFAULT_ALERT_TIMEOUT);
break;
}
case PROGRESS:
case START:
case PROCESSING:
{
res = info();
break;
}
case ERROR:
{
res = error().withAlert("Errore upload");
break;
}
default:
throw new RuntimeException();
}
res = res.withSingleton(info)
.withResponseCode(info.getStatus()
.name());
return res;
}
@RequestMapping(path = "/uploadIframe",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.TEXT_HTML_VALUE)
public final @ResponseBody ResponseEntity<Void> uploadIframe(@RequestParam("file") MultipartFile file, HttpSession session)
{
upload(file, session); //Redirect to Ajax-ready method
return new ResponseEntity<>(HttpStatus.OK);
}
@RequestMapping(value = "/upload",
method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public final PhoenixResponse upload(@RequestParam("file") MultipartFile file, HttpSession session) //Requires modern browser
{
UploadInfo info = (UploadInfo) session.getAttribute(UploadInfo.SESSION_KEY);
log.debug("Upload request received");
FileUploadDTO result;
if (file != null && !file.isEmpty())
{
String uploadFileName = IOUtils.getFileSafeName(file.getOriginalFilename());
log.debug("upload file: " + uploadFileName + " user: " + getUsername());
try
{
FileDescriptor fd;
try (InputStream is = file.getInputStream())
{
fd = fileRepositoryManager.store(is, uploadFileName, AdminModule.ID, getUsername());
}
result = new FileUploadDTO(fd.getName(), fd.getLength(), fd.getMimeType(), fd.getId());
eventDispatcher.multicastEvent(new FileUploadEvent(this, fd));
}
catch (IOException | RuntimeException ex)
{
info.setStatus(UploadState.ERROR);
log.error("Error uploading file", ex);
return error().withAlert(ex.getMessage());
}
}
else
return error();
if (info != null)
info.setStatus(UploadState.DONE);
return info().withSingleton(result);
}
}
说明:我们有两种上传方法。一个支持Ajax,另一个响应POST请求。实际上,该uplodaIframe
方法返回经典的200
空白页。但是由于代码是相同的,所以它将调用重定向到Ajax-ready方法,该方法返回未使用的响应对象
调用时/uploadProgress
,尽管它不使用任何@Autowired
bean,但已设置了所有自动装配。
调用时,/uploadIframe
所有bean都为空,尽管required=false
它们的自动装配上没有
我还在堆栈跟踪中发现了一个有趣的区别:
/ uploadProgress
FileRepositoryController.uploadProgress(HttpSession) line: 106
FileRepositoryController$$FastClassByCGLIB$$7ccffed3.invoke(int, Object, Object[]) line: not available
MethodProxy.invoke(Object, Object[]) line: 204
CglibAopProxy$CglibMethodInvocation.invokeJoinpoint() line: 720
CglibAopProxy$CglibMethodInvocation(ReflectiveMethodInvocation).proceed() line: 157
MethodSecurityInterceptor.invoke(MethodInvocation) line: 68
CglibAopProxy$CglibMethodInvocation(ReflectiveMethodInvocation).proceed() line: 179
CglibAopProxy$DynamicAdvisedInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 655
FileRepositoryController$$EnhancerBySpringCGLIB$$8768d018_2.uploadProgress(HttpSession) line: not available
GeneratedMethodAccessor1992.invoke(Object, Object[]) line: not available
Method.invoke(Object, Object...) line: 606
/ uploadIframe
FileRepositoryController$$EnhancerBySpringCGLIB$$8768d018_2(FileRepositoryController).upload(MultipartFile, HttpSession) line: 144
FileRepositoryController$$EnhancerBySpringCGLIB$$8768d018_2(FileRepositoryController).uploadIframe(MultipartFile, HttpSession) line: 118
GeneratedMethodAccessor1993.invoke(Object, Object[]) line: not available
Method.invoke(Object, Object...) line: 606
我不完全理解为什么,但是问题与final
两种方法的修饰符有关。
卸下后final
,豆子自动接线正确。@ M.Deinum的荣誉
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句