我已经创建了一个签名的applet,用于将文件上传到服务器。代码运行良好,但是我想将该文件从applet发送到服务器端控制器,在该代码中,该控制器已将文件保存到服务器。
我的已签名Applet中的SendFile代码:
public static void sendFile(String destFileName) throws IOException { String filePrivacy = "Public"; String fileKeyword = "uploadFileDocumentName"; String fileComments = "fileComments"; String fileType = "txt"; String fileFolder = "/Works"; String fileDetails = ""; HttpClient client = new HttpClient(); PostMethod postMethod = new PostMethod( "http://localhost:8080/fileUpload/encryptFileUpload.works?filePrivacy="+filePrivacy+"&fileKeyword="+fileKeyword+"&fileComments="+fileComments+"&fileType="+fileType+"&fileFolder="+fileFolder+"&fileDetails="+fileDetails); File f = new File(destFileName); Part[] parts = {new FilePart(f.getName(), f)}; postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); postMethod.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1"); client.executeMethod(postMethod); postMethod.releaseConnection(); }
我的UploadController的方法如下所示:
@RequestMapping(value = "/encryptFileUpload.works") public String uploadEncryptFile(String filePrivacy, String fileKeyword, String fileComments, String fileType, String fileFolder, HttpServletRequest request, HttpServletResponse response) { try { Map<String, Object> requestMap = new HashMap<String, Object>(); requestMap.put(DMSConstants.JCR_FILE_PRIVACY, filePrivacy); requestMap.put(DMSConstants.JCR_FILE_KEYWORD, fileKeyword); requestMap.put(DMSConstants.JCR_FILE_COMMENTS, fileComments); requestMap.put(DMSConstants.JCR_FILE_TYPE, fileType); MultipartHttpServletRequest m = (MultipartHttpServletRequest) request; MultipartFile file = m.getFile("Filedata"); Node folderNode = contentPublishService.getFolderNode(fileFolder); Node node = contentPublishService.saveFileToRepository(folderNode, file.getInputStream(), file.getOriginalFilename(), requestMap); } catch (RepositoryException e) { e.printStackTrace(); return null; }
在行上,MultipartHttpServletRequest m = (MultipartHttpServletRequest) request;我得到异常,如:
MultipartHttpServletRequest m = (MultipartHttpServletRequest) request;
java.lang.ClassCastException: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest at com.nmmc.works.web.controller.FileUploadController.uploadEncryptFile(FileUploadController.java:177) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)
所以哪里出了问题,我应该在代码中进行哪些更改。第二件事是谁可以向控制器获取文件?
需要更多详细信息…
只是一些建议…
什么是 零件 对象?
如果您想按“部分”上传文件,我可能建议您仅重写 MultipartEntity writeTo方法以上传大文件,而不是使用数组,否则不是吗?
关于演员表…我可能猜想这可能会导致问题
通常,HttpClient与FileUpload lib协同工作。那么,为什么不使用它呢?
还有一件事…您将内容mime指向,text/xml但是它是xml,特别是如果它是bin文件的一部分?难道不是某种application/octet- stream替代吗?
text/xml
application/octet- stream
无论如何,这将有助于您在问题中提供更多详细信息