users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 27 Feb 2009 11:28:34 +0100

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
>