users@jersey.java.net

Re: [Jersey] Upload a file from form input,But hava got file is bad.

From: beanor <beanor_at_gmail.com>
Date: Mon, 2 Mar 2009 21:32:58 -0800 (PST)

hi Paul:

First, I'm very thank you for your help. Your code is working nice.But I
have got another problem, follow is very code:

   @Path("form-data")
   @Consumes(MediaType.MULTIPART_FORM_DATA)
   @POST
   public void uploadUrlFormData(
           @FormParam("file") InputStream file,
           @FormParam("file") FormDataContentDisposition cd
           ) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read = 0;
        byte[] buf = new byte[4096];
        while ((read = file.read(buf)) != -1) {
            baos.write(buf, 0, read);
        }
        byte[] byteArr = baos.toByteArray();
        File f = new File("c:/a.rar");
        FileOutputStream fos = new FileOutputStream(f);
        BufferedOutputStream bof = new BufferedOutputStream(fos);
        bof.write(byteArr);
        bof.close();
        bof.flush();
        fos.close();
        baos.close();
        file.close();
        System.out.println("form-data file name: " + new
String(cd.getFileName().getBytes(),"utf-8"));
   }


new String(cd.getFileName().getBytes(),"utf-8")

I want get the uploading file name, But this will get a full path, And
another problem is that the chinese char is not deal, I had got a bad
string.

Any help?


Paul Sandoz wrote:
>
> Hi,
>
> The problem is two fold:
>
> 1) Jersey is not informing you of an error for your method:
>
> @POST public void upload(@FormParam("file") InputStream file)
>
> The above is invalid for the media type "application/x-www-form-
> urlencoded" and
> a Java type of InputStream.
>
> What is happening is Jersey is processing the method as if it were:
>
> @POST public void upload(InputStream file)
>
> and the file you create contains the contents of the HTTP request
> entity, which is the
> encoded form parameters send by the browser.
>
> Jersey needs to throw an error telling you what is wrong. Could
> you log an issue?
>
>
> 2) You are using the wrong enctype value. See here:
>
> http://www.w3.org/TR/html401/interact/forms.html#adef-enctype
>
> which states:
>
> enctype = content-type [CI]
> This attribute specifies the content type used to submit
> the form to the server (when the value of method is
> "post"). The default value for this attribute is
> "application/x-www-form-urlencoded". The value
> "multipart/form-data" should be used in combination with
> the INPUT element, type="file".
>
> when you use the an enctype of "application/x-www-form-
> urlencoded" then i observe that firefox
> sends the file name as the value of the "file" parameter.
>
> You need to use an "enctype" of "multipart/form-data". Then the
> contents of the file will be sent as a body
> part identified by the form parameter name "file".
>
>
> I have attached a very simple maven web application that exercises
> both cases.
>
> Hope this helps,
> Paul.
>
>
>
>
>
>
>
> On Feb 27, 2009, at 8:29 AM, beanor wrote:
>
>>
>> Follow is my code:
>>
>> Class file:
>>
>> @Path("/upload")
>> public class UploadResource {
>> @Path("/file1")
>> @POST
>> public void upload(@FormParam("file")
>> InputStream file) {
>> File f = new File("c:/test.txt");
>> BufferedInputStream bis = null;
>> FileOutputStream fos = null;
>> BufferedOutputStream bos = null;
>> try {
>> fos = new FileOutputStream(f);
>> bos = new BufferedOutputStream(fos);
>> bis = new BufferedInputStream(file);
>> byte[] b = new byte[1024];
>> int byteread = 0;
>> while ((byteread = bis.read(b)) != -1) {
>> bos.write(b, 0, byteread);
>> }
>> } catch (Exception e) {
>> e.printStackTrace();
>> } finally {
>> try {
>> bos.close();
>> fos.close();
>> bis.close();
>> file.close();
>> } catch (Exception e) {
>> e.printStackTrace();
>> }
>> }
>> }
>> }
>>
>> Jsp file:
>>
>> <%@ page language="java" contentType="text/html; charset=utf-8"
>> pageEncoding="utf-8"%>
>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>> "http://www.w3.org/TR/html4/loose.dtd">
>> <html>
>> <head>
>> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
>> <title>Insert title here</title>
>> </head>
>> <body>
>> <form action="/jerseydemo/resources/upload/file1"
>> enctype="application/x-www-form-urlencoded" method="post">
>> <input type="file" name="file" />
>> <input type="submit" name="submit" value="upload" />
>> </form>
>> </body>
>> </html>
>>
>> I had uploaded a file cross the jsp page, But I had got the uploaded
>> file
>> from c:/test.txt,But tthe file is bad(file content is not right),And
>> *.rar
>> file is not open.
>>
>> why?
>> --
>> View this message in context:
>> http://n2.nabble.com/Upload-a-file-from-form-input%2CBut-hava-got-file-is-bad.-tp2394319p2394319.html
>> Sent from the Jersey mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
View this message in context: http://n2.nabble.com/Upload-a-file-from-form-input%2CBut-hava-got-file-is-bad.-tp2394319p2413598.html
Sent from the Jersey mailing list archive at Nabble.com.