我创建了一个用于将文件上传到服务器的签名小程序。代码运行良好,但我想将该文件从小程序发送到服务器端控制器,代码已放置在该控制器中以将该文件保存到服务器。
我在签名 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;
,我遇到了异常,例如:
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)
那么哪里出了问题,我应该在我的代码中做些什么改变。第二件事是我可以在控制器处获取文件吗?