You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by otsuka <t-...@ijk.com> on 2003/05/09 10:11:01 UTC

How to get the file path of uploaded temporary file

Hi,

In order to check the width and height of an image,
I need to get a File object of uploaded image file.
But org.apache.struts.upload.FormFile interface
does not provide any method that tells me the
temporary file's path on a server.

How can I get to know the file path of an uploaded file?

DiskFile class has a method "getFilePath()".
Therefore, if DiskFile class is always used as an
implementing class of FormFile interface, I will
use the class instead of FormFile interface. But
I don't know whether is used as the implementing
class, DiskFile or CommonsMultipartRequestHandler.CommonsFormFile.



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: How to get the file path of uploaded temporary file

Posted by Martin Cooper <ma...@apache.org>.
"otsuka" <t-...@ijk.com> wrote in message
news:007b01c31602$8f9fdc30$0600a8c0@yoda...
> Hi,
>
> In order to check the width and height of an image,
> I need to get a File object of uploaded image file.
> But org.apache.struts.upload.FormFile interface
> does not provide any method that tells me the
> temporary file's path on a server.

You are assuming that there *is* a file on the server. Whether that is true
or not depends on the underlying multipart handler implementation. In Struts
1.1, the default implementation is based on Commons FileUpload, which
retains smaller files in memory, and writes larger ones to disk. That being
the case, the only reliable way of accessing the data is to use code similar
to that suggested by Harm de Laat in a previous reply.

--
Martin Cooper


>
> How can I get to know the file path of an uploaded file?
>
> DiskFile class has a method "getFilePath()".
> Therefore, if DiskFile class is always used as an
> implementing class of FormFile interface, I will
> use the class instead of FormFile interface. But
> I don't know whether is used as the implementing
> class, DiskFile or CommonsMultipartRequestHandler.CommonsFormFile.




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: How to get the file path of uploaded temporary file

Posted by ha...@informatiefabriek.nl.
Hi,

I use something like this:

if (form instanceof UploadForm) {
        UploadForm theForm = (UploadForm) form;
 
        //retrieve the file representation
        FormFile file = theForm.getFile();
 
        //retrieve the file name
        String fileName= file.getFileName();

        //retrieve the content type
        String contentType = file.getContentType();
 
        //retrieve the file size
        String size = ("" + file.getFileSize());
        try {
                //retrieve the file data
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream stream = file.getInputStream();
                //write the file to the file specified
                OutputStream bos = new FileOutputStream("/tmp/" + 
fileName);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                    bos.write(buffer, 0, bytesRead);
                }
                bos.close();
                // data = "The file has been written to \"" + 
theForm.getFilePath() + "\"";
                //close the stream
                stream.close();
 
                }
        catch (FileNotFoundException fnfe) {
                return null;
        }
        catch (IOException ioe) {
                return null;
        }
}

This may help you.

Cheers,

Harm de Laat
Informatiefabriek
The Netherlands





"otsuka" <t-...@ijk.com> 
05/09/2003 10:11 AM
Please respond to
"Struts Users Mailing List" <st...@jakarta.apache.org>


To
<st...@jakarta.apache.org>
cc

Subject
How to get the file path of uploaded temporary file






Hi,

In order to check the width and height of an image,
I need to get a File object of uploaded image file.
But org.apache.struts.upload.FormFile interface
does not provide any method that tells me the
temporary file's path on a server.

How can I get to know the file path of an uploaded file?

DiskFile class has a method "getFilePath()".
Therefore, if DiskFile class is always used as an
implementing class of FormFile interface, I will
use the class instead of FormFile interface. But
I don't know whether is used as the implementing
class, DiskFile or CommonsMultipartRequestHandler.CommonsFormFile.



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org