签名小程序和服务器端控制器之间的通信 [英] Communication between Signed Applet and server side Controller

查看:24
本文介绍了签名小程序和服务器端控制器之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a signed applet for uploading a file to server. 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.

我在签名小程序中的 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; 我收到如下异常:

at the line 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?

推荐答案

需要更多细节...

只是一些建议...

Part 对象是什么?

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

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?

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

Concerning the cast... I may guess that line may cause the problem

MultipartHttpServletRequest m = (MultipartHttpServletRequest) request;

作为一项规则,HttpClient 与 FileUpload 库一起工作.那么为什么不改用它呢?

As a rule HttpClient is working in pare with FileUpload lib. So why not to use it instead?

还有一件事...您将内容 mime 指向 text/xml 但它是 xml,特别是如果它是 bin 文件部分?不应该是某种 application/octet-stream 吗?

And one more thing... you point the content mime as text/xml but is it xml especially if it is a bin file part? Shouldn't it be some kind of application/octet-stream instead ?

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

Anyway, that would be more helpful you to provide more details in your question

这篇关于签名小程序和服务器端控制器之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆