Announcement Announcement Module
Collapse
No announcement yet.
MultipartFile to standard Java File Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MultipartFile to standard Java File

    Hello,

    I would like to leverage the multipartfile support the form tags provide. However, I need to be able to convert the multipartFile to a standard java.io.file. I didn't see anything in the api to do this. Is there a quick and easy way to accomplish this that I am just overlooking?

    Thank you in advance.

  • #2
    MultipartFile has a getBytes() method.
    You can use this to write to any outputstream (FileOutputStream).

    Is that the answer you are looking for? A File in java is just metadata about the file, there is no content to it.

    Comment


    • #3
      Hmmm?

      I am not debating but that was not my understanding of how to use the java.io.File.

      Let me ask you this: if I wanted to pull in a file into the java environment so that i can read the file in order to perform calculations and what not ..... and dont want tie myself to spring, what would i do.

      Basically, I want to be able to pass off the standard java.io.File from my web tier to my service tier.

      I thought I could use the MultiPartFile to upload the users file into the java environment and then covert it to a standard j2se 1.5 java implementation of that file. Of course, I may be dreaming.
      Last edited by latin_spooky; Jun 29th, 2007, 02:18 PM.

      Comment


      • #4
        Anybody have any ideas?

        Just a second post to keep the thread alive. I want to know if anyone knows how I can easitly leverage a MulitpartFile and convert it to a java.io.File. Thank you,

        Chris

        Comment


        • #5
          use the getInputStream method

          Originally posted by latin_spooky View Post
          Just a second post to keep the thread alive. I want to know if anyone knows how I can easitly leverage a MulitpartFile and convert it to a java.io.File. Thank you,

          Chris
          I am beginner in spring though i realize that you can do so by using getFile.getInputStream method from you multipart instance and assign it to any java.io.InputStream. Thus carry the operations on file based in your java environment.

          Regards.

          Comment


          • #6
            MultipartFile file =context.getRequestParameters().getRequiredMultipa rtFile("files[]");

            if (file.getSize() > 0) { // writing file to a directory
            File upLoadedfile = new File(uploadFolder+file.getOriginalFilename());

            upLoadedfile.createNewFile();
            FileOutputStream fos = new FileOutputStream(upLoadedfile);
            fos.write(file.getBytes());
            fos.close(); //setting the value of fileUploaded variable


            This is actually converting multipartfile to java.io.file
            Please clerify if you have byte array or byte stream whats the problem in converting it to java.io.file.

            Comment


            • #7
              I use the following method to get a File which is a copy of the multipart parameter. The file is created on the system's default tmp directory, but it's easy to customize the code to fit your needs:

              Code:
                  public File multipartToFile(MultipartFile multipart) throws IllegalStateException, IOException {
                      File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + 
                                              multipart.getOriginalFilename());
                      multipart.transferTo(tmpFile);
                      return tmpFile;
                  }
              Please note that this method will fail if there is another File with the same name as the Multipart under the temp directory. If you want to avoid this, you should create the tmpFile using File.createTempFile().

              Comment


              • #8
                Originally posted by angelillo View Post
                I use the following method to get a File which is a copy of the multipart parameter. The file is created on the system's default tmp directory, but it's easy to customize the code to fit your needs:

                Code:
                    public File multipartToFile(MultipartFile multipart) throws IllegalStateException, IOException {
                        File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + 
                                                multipart.getOriginalFilename());
                        multipart.transferTo(tmpFile);
                        return tmpFile;
                    }
                Please note that this method will fail if there is another File with the same name as the Multipart under the temp directory. If you want to avoid this, you should create the tmpFile using File.createTempFile().
                very good work

                Comment


                • #9
                  Very good and informative exchange .. Thank you!

                  Comment

                  Working...
                  X