You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Eric Chow <er...@gmail.com> on 2005/01/17 05:12:36 UTC

FileUpload problem !!

Hello,

I used Struts' file upload example to upload file (multipart), it
works file with (UTF-8 description).

I am now going to use  commons-fileupload. But it failed to include a
UTF-8 description, after I uploaded the file and want to display the
description, it just display some unreadable character, cannot display
my input characters.

Any sugegst and idea on it ?

Eric

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


Re: FileUpload problem !!

Posted by Martin Cooper <mf...@gmail.com>.
On Mon, 17 Jan 2005 12:12:36 +0800, Eric Chow <er...@gmail.com> wrote:
> Hello,
> 
> I used Struts' file upload example to upload file (multipart), it
> works file with (UTF-8 description).
> 
> I am now going to use  commons-fileupload. But it failed to include a
> UTF-8 description, after I uploaded the file and want to display the
> description, it just display some unreadable character, cannot display
> my input characters.
> 
> Any sugegst and idea on it ?

Struts uses Commons FileUpload under the covers. So if you can do what
you want with Struts file upload, you can do it with Commons
FileUpload. ;-)

Assuming the description you are talking about is a regular text input
field (or a textarea), make sure you are using the variant of
FileItem.getString() that takes the encoding as a parameter, rather
than the one with no parameters.

--
Martin Cooper


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

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


Re: FileUpload problem !!

Posted by Martin Cooper <mf...@gmail.com>.
On Mon, 17 Jan 2005 15:31:04 -0500, Wade Chandler
<wc...@redesetgrow.com> wrote:
> Martin Cooper wrote:
> > On Mon, 17 Jan 2005 13:22:50 -0500, Wade Chandler
> > <wc...@redesetgrow.com> wrote:
> >
> > <snip/>
> >
> >>What I'm saying is if you read getCharacterEncoding and it returns some
> >>multi-byte encoding calling setCharacterEncoding isn't going to change
> >>the encoding of the request body, and is going to break the file-upload
> >>components ability to parse the request body correctly as it will not be
> >>using the correct character encoding for the internal InputStreamReaders
> >>I'm sure it is using.  I've ran into this issue with readers and form
> >>content before.  Any file-upload developer have any input?
> >
> >
> > Yes, you're correct. Blindly calling setCharacterEncoding() is a bad
> > idea. You at least need to check that getRequestEncoding() returns
> > null first, and you need to be very sure that the request really does
> > use the encoding you set. (On the other hand, most browsers today do
> > not send content encoding information.)
> >
> > Note that Eric's code also invokes the getString() method with no
> > parameters, whereas, as I noted before, he needs to invoke the variant
> > that takes the encoding as a parameter. Fixing that alone will likely
> > get things working.
> >
> > --
> > Martin Cooper
> >
> >
> >
> >>Wade
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
> >
> 
> Looking at the code for FileUpload and how it gets it's headers one
> could also do
> diskFileUpload.setCharacterEncoding(request.getCharacterEncoding()) and
> then shouldn't have to worry about passing the encoding to getString
> because the strings should have been parsed out using substring calls on
> main header string that already had it's encoding set.

I assume you're referring to setHeaderEncoding(). This is different.
This is the encoding for header values themselves, not the encoding
used for the data in the parts. So you still need to invoke the
version of getString() that takes the encoding.

In some (rare) cases, the client may supply the encoding as a header
to each of the parts, in which case the zero-arg version of
getString() will work. Note, however, that most, if not all, browsers
don't do that. If you're using a client such as Commons HttpClient,
though, you can set things up to add encoding headers.

--
Martin Cooper


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

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


Re: FileUpload problem !!

Posted by Wade Chandler <wc...@redesetgrow.com>.
Martin Cooper wrote:
> On Mon, 17 Jan 2005 13:22:50 -0500, Wade Chandler
> <wc...@redesetgrow.com> wrote:
> 
> <snip/>
> 
>>What I'm saying is if you read getCharacterEncoding and it returns some
>>multi-byte encoding calling setCharacterEncoding isn't going to change
>>the encoding of the request body, and is going to break the file-upload
>>components ability to parse the request body correctly as it will not be
>>using the correct character encoding for the internal InputStreamReaders
>>I'm sure it is using.  I've ran into this issue with readers and form
>>content before.  Any file-upload developer have any input?
> 
> 
> Yes, you're correct. Blindly calling setCharacterEncoding() is a bad
> idea. You at least need to check that getRequestEncoding() returns
> null first, and you need to be very sure that the request really does
> use the encoding you set. (On the other hand, most browsers today do
> not send content encoding information.)
> 
> Note that Eric's code also invokes the getString() method with no
> parameters, whereas, as I noted before, he needs to invoke the variant
> that takes the encoding as a parameter. Fixing that alone will likely
> get things working.
> 
> --
> Martin Cooper
> 
> 
> 
>>Wade
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 

Looking at the code for FileUpload and how it gets it's headers one 
could also do 
diskFileUpload.setCharacterEncoding(request.getCharacterEncoding()) and 
then shouldn't have to worry about passing the encoding to getString 
because the strings should have been parsed out using substring calls on 
main header string that already had it's encoding set.

Wade


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


Re: FileUpload problem !!

Posted by Martin Cooper <mf...@gmail.com>.
On Mon, 17 Jan 2005 13:22:50 -0500, Wade Chandler
<wc...@redesetgrow.com> wrote:

<snip/>
> What I'm saying is if you read getCharacterEncoding and it returns some
> multi-byte encoding calling setCharacterEncoding isn't going to change
> the encoding of the request body, and is going to break the file-upload
> components ability to parse the request body correctly as it will not be
> using the correct character encoding for the internal InputStreamReaders
> I'm sure it is using.  I've ran into this issue with readers and form
> content before.  Any file-upload developer have any input?

Yes, you're correct. Blindly calling setCharacterEncoding() is a bad
idea. You at least need to check that getRequestEncoding() returns
null first, and you need to be very sure that the request really does
use the encoding you set. (On the other hand, most browsers today do
not send content encoding information.)

Note that Eric's code also invokes the getString() method with no
parameters, whereas, as I noted before, he needs to invoke the variant
that takes the encoding as a parameter. Fixing that alone will likely
get things working.

--
Martin Cooper


> 
> Wade

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


Re: FileUpload problem !!

Posted by Wade Chandler <wc...@redesetgrow.com>.
Dakota Jack wrote:
> He is setting the character encoding of reading the request.
> 
> Jack
> 
> 
> On Mon, 17 Jan 2005 09:31:56 -0500, Wade Chandler
> <wc...@redesetgrow.com> wrote:
> 
>>Eric Chow wrote:
>>
>>>My Code is as following:
>>>
>>>
>>>
>>><html>
>>><head>
>>><title>File Upload</title>
>>><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
>>></head>
>>><body bgcolor="#FFFFFF" text="#000000"><p><font size="5"color="#FF0000">
>>><b>Upload</b></font></p>
>>>
>>><form name="Form1" enctype="multipart/form-data" method="post"
>>>action="upload.jsp">
>>><p>File 1<input type="file"name="File1" size="20" maxlength="20"> </p>
>>><p>Description 1<input type="text" name="File1" size="30" maxlength="50"> </p>
>>><p>File 2<input type="file" name="File2" size="20" maxlength="20"> </p>
>>><p>Description 2<input type="text" name="File2" size="30" maxlength="50"> </p>
>>><p>File 3<input type="file" name="File3" size="20" maxlength="20"> </p>
>>><p>Description 3<input type="text" name="File3" size="30" maxlength="50"> </p>
>>><p> <input type="submit"value="Upload"> <input type="reset" value="Clear"> </p>
>>></form>
>>>
>>></body>
>>></html>
>>>
>>>
>>>
>>>
>>><?%@ page contentType="text/html;charset=UTF-8" language="java"
>>>pageEncoding="UTF-8"%>
>>><%@ page import="java.io.*" %>
>>><%@ page import="java.util.*" %>
>>><%@ page import="org.apache.commons.fileupload.*" %>
>>>
>>><%
>>>      request.setCharacterEncoding("UTF-8");
>>>
>>>    String saveDirectory = "c:\\";
>>>    String tmpDirectory = "c:\\";
>>>    int maxPostSize = 1024 * 1024;
>>>%>
>>><%
>>>    String FileDescription = null;
>>>    String FileName = null;
>>>    long FileSize = 0;
>>>    String ContentType = null;
>>>    int count = 0 ;
>>>%>
>>>
>>><%
>>>    DiskFileUpload upload = new DiskFileUpload();
>>>
>>>    upload.setSizeThreshold(4096);
>>>
>>>    upload.setSizeMax(maxPostSize);
>>>
>>>    upload.setRepositoryPath(tmpDirectory);
>>>    List items = upload.parseRequest(request);
>>>%>
>>><body>
>>><%
>>> Iterator iter = items.iterator();
>>>
>>>    int tmp = 0;
>>>    FileItem tmpItem = null;
>>> while (iter.hasNext())
>>> {
>>>        tmp++;
>>>        FileItem item = (FileItem) iter.next();
>>>
>>>        if (item.isFormField()) {
>>>            FileDescription = item.getString();
>>>        } else {
>>>            FileName = item.getName();
>>>
>>>  try {
>>>   // for wintel platform
>>>    FileName = FileName.substring(FileName.lastIndexOf("\\")+1);
>>>  // for unix-like platform
>>>    FileName = FileName.substring(FileName.lastIndexOf("/")+1);
>>>  } catch ( Exception ex ) {
>>> out.println(ex);
>>>  }
>>>
>>>
>>>            ContentType = item.getContentType();
>>>            FileSize = item.getSize();
>>>            tmpItem = item;
>>>        }
>>>
>>>  if (tmp == 2 && FileSize != 0)
>>>  {
>>>   count ++;
>>>
>>>%>
>>><font color="red">Total file uploaded: <%= count %></font><br>
>>>File name: <%= FileName %><br>
>>>File size: <%= FileSize %> Bytes<br>
>>>Content type: <%= ContentType %><br>
>>>Description: <%= FileDescription %><br><br>
>>>
>>><%
>>>   try {
>>>      out.println(FileName);
>>>      File uploadedFile = new File(saveDirectory + FileName);
>>>      tmpItem.write(uploadedFile);
>>>   } catch ( Exception ex ) {
>>>      out.println(ex);
>>>    }
>>>
>>>            tmp = 0;
>>>        } else if (tmp == 2 && FileSize == 0) {
>>>            tmp = 0;
>>>  } // end if
>>> } // end while
>>>%>
>>><font color="red"><%= count %></font>
>>></body>
>>></html>
>>>
>>>
>>>
>>>
>>>
>>>
>>>On Sun, 16 Jan 2005 21:08:16 -0800, Dakota Jack <da...@gmail.com> wrote:
>>>
>>>
>>>>Can you give your code?  I am having trouble determining what you are
>>>>having difficulty with here.  Give the code or a better description of
>>>>what you find you cannot do.
>>>>
>>>>Jack
>>>>
>>>>
>>>>On Mon, 17 Jan 2005 12:12:36 +0800, Eric Chow <er...@gmail.com> wrote:
>>>>
>>>>
>>>>>Hello,
>>>>>
>>>>>I used Struts' file upload example to upload file (multipart), it
>>>>>works file with (UTF-8 description).
>>>>>
>>>>>I am now going to use  commons-fileupload. But it failed to include a
>>>>>UTF-8 description, after I uploaded the file and want to display the
>>>>>description, it just display some unreadable character, cannot display
>>>>>my input characters.
>>>>>
>>>>>Any sugegst and idea on it ?
>>>>>
>>>>>Eric
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>>>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>>>
>>>>>
>>>>
>>>>--
>>>>------------------------------
>>>>
>>>>"You can lead a horse to water but you cannot make it float on its back."
>>>>
>>>>~Dakota Jack~
>>>>
>>>>"You can't wake a person who is pretending to be asleep."
>>>>
>>>>~Native Proverb~
>>>>
>>>>"Each man is good in His sight. It is not necessary for eagles to be crows."
>>>>
>>>>~Hunkesni (Sitting Bull), Hunkpapa Sioux~
>>>>
>>>>-----------------------------------------------
>>>>
>>>>"This message may contain confidential and/or privileged information.
>>>>If you are not the addressee or authorized to receive this for the
>>>>addressee, you must not use, copy, disclose, or take any action based
>>>>on this message or any information herein. If you have received this
>>>>message in error, please advise the sender immediately by reply e-mail
>>>>and delete this message. Thank you for your cooperation."
>>>>
>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>
>>>
>>>
>>
>>Why are you setting the character encoding of your request?  The web
>>browser would have done this for you.  You did not specify an
>>accept-charset="UTF-8" attribute in your form in html.  See
>>http://www.w3.org/TR/REC-html40/interact/forms.html for more info and do
>>a search for encoding or accept-charset.  Don't change the request
>>encoding on the server side thinking the server is going to some how be
>>able to transform this encoding.  The encoded text came from you client
>>request.
>>
>>Wade
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
> 
> 
> 
What I'm saying is if you read getCharacterEncoding and it returns some 
multi-byte encoding calling setCharacterEncoding isn't going to change 
the encoding of the request body, and is going to break the file-upload 
components ability to parse the request body correctly as it will not be 
using the correct character encoding for the internal InputStreamReaders 
I'm sure it is using.  I've ran into this issue with readers and form 
content before.  Any file-upload developer have any input?

Wade


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


Re: FileUpload problem !!

Posted by Dakota Jack <da...@gmail.com>.
He is setting the character encoding of reading the request.

Jack


On Mon, 17 Jan 2005 09:31:56 -0500, Wade Chandler
<wc...@redesetgrow.com> wrote:
> Eric Chow wrote:
> > My Code is as following:
> >
> >
> >
> > <html>
> > <head>
> > <title>File Upload</title>
> > <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
> > </head>
> > <body bgcolor="#FFFFFF" text="#000000"><p><font size="5"color="#FF0000">
> > <b>Upload</b></font></p>
> >
> > <form name="Form1" enctype="multipart/form-data" method="post"
> > action="upload.jsp">
> > <p>File 1<input type="file"name="File1" size="20" maxlength="20"> </p>
> > <p>Description 1<input type="text" name="File1" size="30" maxlength="50"> </p>
> > <p>File 2<input type="file" name="File2" size="20" maxlength="20"> </p>
> > <p>Description 2<input type="text" name="File2" size="30" maxlength="50"> </p>
> > <p>File 3<input type="file" name="File3" size="20" maxlength="20"> </p>
> > <p>Description 3<input type="text" name="File3" size="30" maxlength="50"> </p>
> > <p> <input type="submit"value="Upload"> <input type="reset" value="Clear"> </p>
> > </form>
> >
> > </body>
> > </html>
> >
> >
> >
> >
> > <?%@ page contentType="text/html;charset=UTF-8" language="java"
> > pageEncoding="UTF-8"%>
> > <%@ page import="java.io.*" %>
> > <%@ page import="java.util.*" %>
> > <%@ page import="org.apache.commons.fileupload.*" %>
> >
> > <%
> >       request.setCharacterEncoding("UTF-8");
> >
> >     String saveDirectory = "c:\\";
> >     String tmpDirectory = "c:\\";
> >     int maxPostSize = 1024 * 1024;
> > %>
> > <%
> >     String FileDescription = null;
> >     String FileName = null;
> >     long FileSize = 0;
> >     String ContentType = null;
> >     int count = 0 ;
> > %>
> >
> > <%
> >     DiskFileUpload upload = new DiskFileUpload();
> >
> >     upload.setSizeThreshold(4096);
> >
> >     upload.setSizeMax(maxPostSize);
> >
> >     upload.setRepositoryPath(tmpDirectory);
> >     List items = upload.parseRequest(request);
> > %>
> > <body>
> > <%
> >  Iterator iter = items.iterator();
> >
> >     int tmp = 0;
> >     FileItem tmpItem = null;
> >  while (iter.hasNext())
> >  {
> >         tmp++;
> >         FileItem item = (FileItem) iter.next();
> >
> >         if (item.isFormField()) {
> >             FileDescription = item.getString();
> >         } else {
> >             FileName = item.getName();
> >
> >   try {
> >    // for wintel platform
> >     FileName = FileName.substring(FileName.lastIndexOf("\\")+1);
> >   // for unix-like platform
> >     FileName = FileName.substring(FileName.lastIndexOf("/")+1);
> >   } catch ( Exception ex ) {
> >  out.println(ex);
> >   }
> >
> >
> >             ContentType = item.getContentType();
> >             FileSize = item.getSize();
> >             tmpItem = item;
> >         }
> >
> >   if (tmp == 2 && FileSize != 0)
> >   {
> >    count ++;
> >
> > %>
> > <font color="red">Total file uploaded: <%= count %></font><br>
> > File name: <%= FileName %><br>
> > File size: <%= FileSize %> Bytes<br>
> > Content type: <%= ContentType %><br>
> > Description: <%= FileDescription %><br><br>
> >
> > <%
> >    try {
> >       out.println(FileName);
> >       File uploadedFile = new File(saveDirectory + FileName);
> >       tmpItem.write(uploadedFile);
> >    } catch ( Exception ex ) {
> >       out.println(ex);
> >     }
> >
> >             tmp = 0;
> >         } else if (tmp == 2 && FileSize == 0) {
> >             tmp = 0;
> >   } // end if
> >  } // end while
> > %>
> > <font color="red"><%= count %></font>
> > </body>
> > </html>
> >
> >
> >
> >
> >
> >
> > On Sun, 16 Jan 2005 21:08:16 -0800, Dakota Jack <da...@gmail.com> wrote:
> >
> >>Can you give your code?  I am having trouble determining what you are
> >>having difficulty with here.  Give the code or a better description of
> >>what you find you cannot do.
> >>
> >>Jack
> >>
> >>
> >>On Mon, 17 Jan 2005 12:12:36 +0800, Eric Chow <er...@gmail.com> wrote:
> >>
> >>>Hello,
> >>>
> >>>I used Struts' file upload example to upload file (multipart), it
> >>>works file with (UTF-8 description).
> >>>
> >>>I am now going to use  commons-fileupload. But it failed to include a
> >>>UTF-8 description, after I uploaded the file and want to display the
> >>>description, it just display some unreadable character, cannot display
> >>>my input characters.
> >>>
> >>>Any sugegst and idea on it ?
> >>>
> >>>Eric
> >>>
> >>>---------------------------------------------------------------------
> >>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> >>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >>>
> >>>
> >>
> >>--
> >>------------------------------
> >>
> >>"You can lead a horse to water but you cannot make it float on its back."
> >>
> >>~Dakota Jack~
> >>
> >>"You can't wake a person who is pretending to be asleep."
> >>
> >>~Native Proverb~
> >>
> >>"Each man is good in His sight. It is not necessary for eagles to be crows."
> >>
> >>~Hunkesni (Sitting Bull), Hunkpapa Sioux~
> >>
> >>-----------------------------------------------
> >>
> >>"This message may contain confidential and/or privileged information.
> >>If you are not the addressee or authorized to receive this for the
> >>addressee, you must not use, copy, disclose, or take any action based
> >>on this message or any information herein. If you have received this
> >>message in error, please advise the sender immediately by reply e-mail
> >>and delete this message. Thank you for your cooperation."
> >>
> >
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
> > 
> Why are you setting the character encoding of your request?  The web
> browser would have done this for you.  You did not specify an
> accept-charset="UTF-8" attribute in your form in html.  See
> http://www.w3.org/TR/REC-html40/interact/forms.html for more info and do
> a search for encoding or accept-charset.  Don't change the request
> encoding on the server side thinking the server is going to some how be
> able to transform this encoding.  The encoded text came from you client
> request.
> 
> Wade
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
------------------------------

"You can lead a horse to water but you cannot make it float on its back."

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

-----------------------------------------------

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

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


Re: FileUpload problem !!

Posted by Wade Chandler <wc...@redesetgrow.com>.
Eric Chow wrote:
> My Code is as following:
> 
> 
> 
> <html>
> <head>
> <title>File Upload</title>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
> </head>
> <body bgcolor="#FFFFFF" text="#000000"><p><font size="5"color="#FF0000">
> <b>Upload</b></font></p>
>  
> <form name="Form1" enctype="multipart/form-data" method="post"
> action="upload.jsp">
> <p>File 1<input type="file"name="File1" size="20" maxlength="20"> </p>
> <p>Description 1<input type="text" name="File1" size="30" maxlength="50"> </p>
> <p>File 2<input type="file" name="File2" size="20" maxlength="20"> </p>
> <p>Description 2<input type="text" name="File2" size="30" maxlength="50"> </p>
> <p>File 3<input type="file" name="File3" size="20" maxlength="20"> </p>
> <p>Description 3<input type="text" name="File3" size="30" maxlength="50"> </p>
> <p> <input type="submit"value="Upload"> <input type="reset" value="Clear"> </p>
> </form>
>  
> </body>
> </html>
> 
> 
> 
> 
> <?%@ page contentType="text/html;charset=UTF-8" language="java"
> pageEncoding="UTF-8"%>
> <%@ page import="java.io.*" %>
> <%@ page import="java.util.*" %>
> <%@ page import="org.apache.commons.fileupload.*" %>
>  
> <% 
>  	request.setCharacterEncoding("UTF-8");
>  
>     String saveDirectory = "c:\\";
>     String tmpDirectory = "c:\\";
>     int maxPostSize = 1024 * 1024; 
> %>
> <%
>     String FileDescription = null; 
>     String FileName = null;
>     long FileSize = 0;
>     String ContentType = null;
>     int count = 0 ;
> %>
>  
> <% 
>     DiskFileUpload upload = new DiskFileUpload();
>     
>     upload.setSizeThreshold(4096);
>        
>     upload.setSizeMax(maxPostSize);
>       
>     upload.setRepositoryPath(tmpDirectory);
>     List items = upload.parseRequest(request);
> %>
> <body>
> <% 
>  Iterator iter = items.iterator();
>  
>     int tmp = 0;
>     FileItem tmpItem = null;
>  while (iter.hasNext()) 
>  { 
>         tmp++;
>         FileItem item = (FileItem) iter.next();
>         
>         if (item.isFormField()) {        
>             FileDescription = item.getString();            
>         } else {        
>             FileName = item.getName();
>   
>   try {
>    // for wintel platform
>     FileName = FileName.substring(FileName.lastIndexOf("\\")+1);
>   // for unix-like platform
>     FileName = FileName.substring(FileName.lastIndexOf("/")+1); 
>   } catch ( Exception ex ) {
>  out.println(ex);
>   }
>  
> 
>             ContentType = item.getContentType();
>             FileSize = item.getSize();
>             tmpItem = item;        
>         }
>         
>   if (tmp == 2 && FileSize != 0)
>   { 
>    count ++;
>  
> %>
> <font color="red">Total file uploaded: <%= count %></font><br>
> File name: <%= FileName %><br>
> File size: <%= FileSize %> Bytes<br>
> Content type: <%= ContentType %><br>
> Description: <%= FileDescription %><br><br>
>  
> <%        
>    try {
>       out.println(FileName);
>       File uploadedFile = new File(saveDirectory + FileName);
>       tmpItem.write(uploadedFile);
>    } catch ( Exception ex ) {
>       out.println(ex);
>     }
>             
>             tmp = 0;
>         } else if (tmp == 2 && FileSize == 0) {
>             tmp = 0;
>   } // end if 
>  } // end while
> %> 
> <font color="red"><%= count %></font>
> </body>
> </html>
> 
> 
> 
> 
> 
> 
> On Sun, 16 Jan 2005 21:08:16 -0800, Dakota Jack <da...@gmail.com> wrote:
> 
>>Can you give your code?  I am having trouble determining what you are
>>having difficulty with here.  Give the code or a better description of
>>what you find you cannot do.
>>
>>Jack
>>
>>
>>On Mon, 17 Jan 2005 12:12:36 +0800, Eric Chow <er...@gmail.com> wrote:
>>
>>>Hello,
>>>
>>>I used Struts' file upload example to upload file (multipart), it
>>>works file with (UTF-8 description).
>>>
>>>I am now going to use  commons-fileupload. But it failed to include a
>>>UTF-8 description, after I uploaded the file and want to display the
>>>description, it just display some unreadable character, cannot display
>>>my input characters.
>>>
>>>Any sugegst and idea on it ?
>>>
>>>Eric
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>
>>>
>>
>>--
>>------------------------------
>>
>>"You can lead a horse to water but you cannot make it float on its back."
>>
>>~Dakota Jack~
>>
>>"You can't wake a person who is pretending to be asleep."
>>
>>~Native Proverb~
>>
>>"Each man is good in His sight. It is not necessary for eagles to be crows."
>>
>>~Hunkesni (Sitting Bull), Hunkpapa Sioux~
>>
>>-----------------------------------------------
>>
>>"This message may contain confidential and/or privileged information.
>>If you are not the addressee or authorized to receive this for the
>>addressee, you must not use, copy, disclose, or take any action based
>>on this message or any information herein. If you have received this
>>message in error, please advise the sender immediately by reply e-mail
>>and delete this message. Thank you for your cooperation."
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 
Why are you setting the character encoding of your request?  The web 
browser would have done this for you.  You did not specify an 
accept-charset="UTF-8" attribute in your form in html.  See 
http://www.w3.org/TR/REC-html40/interact/forms.html for more info and do 
a search for encoding or accept-charset.  Don't change the request 
encoding on the server side thinking the server is going to some how be 
able to transform this encoding.  The encoded text came from you client 
request.

Wade


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


Re: FileUpload problem !!

Posted by Eric Chow <er...@gmail.com>.
My Code is as following:



<html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000"><p><font size="5"color="#FF0000">
<b>Upload</b></font></p>
 
<form name="Form1" enctype="multipart/form-data" method="post"
action="upload.jsp">
<p>File 1<input type="file"name="File1" size="20" maxlength="20"> </p>
<p>Description 1<input type="text" name="File1" size="30" maxlength="50"> </p>
<p>File 2<input type="file" name="File2" size="20" maxlength="20"> </p>
<p>Description 2<input type="text" name="File2" size="30" maxlength="50"> </p>
<p>File 3<input type="file" name="File3" size="20" maxlength="20"> </p>
<p>Description 3<input type="text" name="File3" size="30" maxlength="50"> </p>
<p> <input type="submit"value="Upload"> <input type="reset" value="Clear"> </p>
</form>
 
</body>
</html>




<?%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
 
<% 
 	request.setCharacterEncoding("UTF-8");
 
    String saveDirectory = "c:\\";
    String tmpDirectory = "c:\\";
    int maxPostSize = 1024 * 1024; 
%>
<%
    String FileDescription = null; 
    String FileName = null;
    long FileSize = 0;
    String ContentType = null;
    int count = 0 ;
%>
 
<% 
    DiskFileUpload upload = new DiskFileUpload();
    
    upload.setSizeThreshold(4096);
       
    upload.setSizeMax(maxPostSize);
      
    upload.setRepositoryPath(tmpDirectory);
    List items = upload.parseRequest(request);
%>
<body>
<% 
 Iterator iter = items.iterator();
 
    int tmp = 0;
    FileItem tmpItem = null;
 while (iter.hasNext()) 
 { 
        tmp++;
        FileItem item = (FileItem) iter.next();
        
        if (item.isFormField()) {        
            FileDescription = item.getString();            
        } else {        
            FileName = item.getName();
  
  try {
   // for wintel platform
    FileName = FileName.substring(FileName.lastIndexOf("\\")+1);
  // for unix-like platform
    FileName = FileName.substring(FileName.lastIndexOf("/")+1); 
  } catch ( Exception ex ) {
 out.println(ex);
  }
 

            ContentType = item.getContentType();
            FileSize = item.getSize();
            tmpItem = item;        
        }
        
  if (tmp == 2 && FileSize != 0)
  { 
   count ++;
 
%>
<font color="red">Total file uploaded: <%= count %></font><br>
File name: <%= FileName %><br>
File size: <%= FileSize %> Bytes<br>
Content type: <%= ContentType %><br>
Description: <%= FileDescription %><br><br>
 
<%        
   try {
      out.println(FileName);
      File uploadedFile = new File(saveDirectory + FileName);
      tmpItem.write(uploadedFile);
   } catch ( Exception ex ) {
      out.println(ex);
    }
            
            tmp = 0;
        } else if (tmp == 2 && FileSize == 0) {
            tmp = 0;
  } // end if 
 } // end while
%> 
<font color="red"><%= count %></font>
</body>
</html>






On Sun, 16 Jan 2005 21:08:16 -0800, Dakota Jack <da...@gmail.com> wrote:
> Can you give your code?  I am having trouble determining what you are
> having difficulty with here.  Give the code or a better description of
> what you find you cannot do.
> 
> Jack
> 
> 
> On Mon, 17 Jan 2005 12:12:36 +0800, Eric Chow <er...@gmail.com> wrote:
> > Hello,
> >
> > I used Struts' file upload example to upload file (multipart), it
> > works file with (UTF-8 description).
> >
> > I am now going to use  commons-fileupload. But it failed to include a
> > UTF-8 description, after I uploaded the file and want to display the
> > description, it just display some unreadable character, cannot display
> > my input characters.
> >
> > Any sugegst and idea on it ?
> >
> > Eric
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
> 
> --
> ------------------------------
> 
> "You can lead a horse to water but you cannot make it float on its back."
> 
> ~Dakota Jack~
> 
> "You can't wake a person who is pretending to be asleep."
> 
> ~Native Proverb~
> 
> "Each man is good in His sight. It is not necessary for eagles to be crows."
> 
> ~Hunkesni (Sitting Bull), Hunkpapa Sioux~
> 
> -----------------------------------------------
> 
> "This message may contain confidential and/or privileged information.
> If you are not the addressee or authorized to receive this for the
> addressee, you must not use, copy, disclose, or take any action based
> on this message or any information herein. If you have received this
> message in error, please advise the sender immediately by reply e-mail
> and delete this message. Thank you for your cooperation."
>

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


Re: FileUpload problem !!

Posted by Dakota Jack <da...@gmail.com>.
Can you give your code?  I am having trouble determining what you are
having difficulty with here.  Give the code or a better description of
what you find you cannot do.

Jack


On Mon, 17 Jan 2005 12:12:36 +0800, Eric Chow <er...@gmail.com> wrote:
> Hello,
> 
> I used Struts' file upload example to upload file (multipart), it
> works file with (UTF-8 description).
> 
> I am now going to use  commons-fileupload. But it failed to include a
> UTF-8 description, after I uploaded the file and want to display the
> description, it just display some unreadable character, cannot display
> my input characters.
> 
> Any sugegst and idea on it ?
> 
> Eric
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
------------------------------

"You can lead a horse to water but you cannot make it float on its back."

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

-----------------------------------------------

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

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