2012-08-01 78 views
1

我已经创建了一个用于将文件上传到服务器的签名小程序。代码运行良好,但我想从小程序发送该文件到服务器端控制器,代码已将该文件保存到服务器。签名的Applet和服务器端控制器之间的通信

我SENDFILE代码在签名Applet:

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) 

那么哪里出了问题,我应该在代码中做些什么改变。第二件事是我可以在控制器上获得文件?

回答

0

需要更多的细节......

只是一些建议...

什么是部分对象?

如果你想上传文件'部分'我可能会建议只是为了覆盖MultipartEntity writeTo方法来上传大文件,而不是使用数组或可能不是事情?

就铸造......我可以猜测该行可能会导致问题

MultipartHttpServletRequest m = (MultipartHttpServletRequest) request; 

按规定的HttpClient正在与LIB文件上传削减。那么为什么不使用它呢?

还有一件事......你指出内容MIME为text/xml,但它是XML,尤其是如果它是一个bin文件的一部分?它不应该是某种application/octet-stream而是?

无论如何,这将是更有帮助你在你的问题

1

我得到了该问题的解决方案提供更多的细节。在这里我在我现有的代码已经改变

我的签名Applet:

MultipartPostMethod mPost = new MultipartPostMethod(uri);  
mPost.addParameter("Filedata", f.getName(), f); 
client.executeMethod(mPost); 

现在,它的工作的罚款。