简体   繁体   English

Signed Applet与服务器端控制器之间的通信

[英]Communication between Signed Applet and server side Controller

I have created a signed applet for uploading a file to server. 我已经创建了一个签名的applet,用于将文件上传到服务器。 Code is running fine but I want to send that file from applet to server side controller where the code has placed to save that file to server. 代码运行良好,但是我想将该文件从applet发送到服务器端控制器,在该代码中,该文件已保存到服务器。

My SendFile Code in signed 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();
    }

And My UploadController's method looks like: 我的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;
    }

at the line MultipartHttpServletRequest m = (MultipartHttpServletRequest) request; 在行MultipartHttpServletRequest m = (MultipartHttpServletRequest) request; I am getting exception like: 我收到类似的异常:

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)

So where is going wrong and what changes should i do in my code. 所以哪里出了问题,我应该在代码中进行哪些更改。 And second thing is who can i get file at controller? 第二件事是谁可以向控制器获取文件?

I got the solution for that problem. 我找到了解决该问题的方法。 here what I have changed in my existing code 这是我在现有代码中所做的更改

My Signed Applet: 我的签名小程序:

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

Now its working fine. 现在工作正常。

Need more details... 需要更多详细信息...

Just some suggestions... 只是一些建议...

What is the Part object is? 什么是零件对象?

If you want to upload file by 'parts' I may recommend just to override MultipartEntity writeTo method to upload big file instead of using arrays or maybe it is not the thing? 如果您想按“部分”上传文件,我可能建议您仅重写MultipartEntity writeTo方法以上传大文件,而不是使用数组,否则不是吗?

Concerning the cast... I may guess that line may cause the problem 关于演员表...我可能猜想这可能会导致问题

MultipartHttpServletRequest m = (MultipartHttpServletRequest) request;

As a rule HttpClient is working in pare with FileUpload lib. 通常,HttpClient与FileUpload lib协同工作。 So why not to use it instead? 那么,为什么不使用它呢?

And one more thing... you point the content mime as text/xml but is it xml especially if it is a bin file part? 还有一件事...您将内容mime指向text/xml但是它是xml,特别是如果它是bin文件的一部分? Shouldn't it be some kind of application/octet-stream instead ? 应该不是某种application/octet-stream吗?

Anyway, that would be more helpful you to provide more details in your question 无论如何,这将有助于您在问题中提供更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM