1

我创建了一个用于将文件上传到服务器的签名小程序。代码运行良好,但我想将该文件从小程序发送到服务器端控制器,代码已放置在该控制器中以将该文件保存到服务器。

我在签名 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)

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

4

2 回答 2

1

我得到了那个问题的解决方案。这是我在现有代码中所做的更改

我的签名小程序:

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

现在它工作正常。

于 2012-08-10T08:49:56.487 回答
0

需要更多细节...

只是一些建议...

Part对象是什么?

如果您想按“部分”上传文件,我可能建议您只覆盖MultipartEntity writeTo 方法来上传大文件而不是使用数组,或者这不是问题?

关于演员表......我可能猜想那条线可能会导致问题

MultipartHttpServletRequest m = (MultipartHttpServletRequest) request;

作为一项规则,HttpClient 与 FileUpload 库配合使用。那么为什么不使用它呢?

还有一件事……您将内容 mime 指向为text/xmlxml,尤其是当它是 bin 文件部分时?不应该是某种application/octet-stream代替吗?

无论如何,在您的问题中提供更多详细信息会更有帮助

于 2012-08-01T20:02:27.000 回答