1

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.

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

And My UploadController's method looks like:

@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; 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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Balasaheb
  • 625
  • 2
  • 12
  • 33

2 Answers2

1

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.

Balasaheb
  • 625
  • 2
  • 12
  • 33
0

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?

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. 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? 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

user592704
  • 3,674
  • 11
  • 70
  • 107