Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:44:58+00:00 2026-06-17T11:44:58+00:00

I want to upload a file using GWT to REST web service. But I

  • 0

I want to upload a file using GWT to REST web service. But I got null value in these following fields.

InputStream uploadedInputStream, FormDataContentDisposition fileDetail

Following is my GWT client code

public void onModuleLoad() {
        Button button = new Button("Click Here");
        final FormPanel form = new FormPanel();
        final FileUpload fileUpload = new FileUpload();
        form.setMethod(FormPanel.METHOD_POST);
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        fileUpload.setName("upload");
        form.add(fileUpload);
        form.setAction("http://localhost:8080/RestWeb/webresources/generic/upload");
        RootPanel.get().add(form);
        RootPanel.get().add(button);
        button.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                System.out.println(fileUpload.getFilename());
                System.out.println(fileUpload.getName());
                System.out.println(fileUpload.getStyleName());
                fileUpload.setEnabled(true);

                System.out.println(form.getTarget());
                form.submit();
            }
        });
        form.addSubmitHandler(new SubmitHandler() {

            @Override
            public void onSubmit(SubmitEvent event) {
        Window.alert("Onsubmit");

            }
        });
        form.addSubmitCompleteHandler(new SubmitCompleteHandler() {

            @Override
            public void onSubmitComplete(SubmitCompleteEvent event) {
                Window.alert("OnsubmitComplete"+event.getResults());

            }
        });
    }
}

Following is my server side code. I got connection between client and server but got null value only. I don’t understand my mistake. I refer many sites but I got same code.

@Path("generic")
@WebService
public class GenericResource {

    @Context
    private UriInfo context;
    @Context
    private HttpServletResponse response;
    private String content = "content";

    /**
     * Creates a new instance of GenericResource
     */
    public GenericResource() {
    }
 @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)

    public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {

        String uploadedFileLocation = "C://Users/SPC/Parvathy/upload" + fileDetail.getFileName();

        // save it
        writeToFile(uploadedInputStream, uploadedFileLocation);

        String output = "File uploaded to : " + uploadedFileLocation;

        return Response.status(200).entity(output).build();

    }

    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream,
            String uploadedFileLocation) {

        try {
            OutputStream out = new FileOutputStream(new File(
                    uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
      }
    }

    public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
      //  if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
            // CORS "pre-flight" request
                response.addHeader("Content-Type",
                "multipart/form-data");
            response.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8888");
            response.addHeader("Access-Control-Allow-Methods",  "PUT, GET, POST, DELETE, OPTIONS");
            response.addHeader("Access-Control-Allow-Headers","origin, access-control-allow-methods, content-type, access-control-allow-origin, access-control-allow-headers");
            response.addHeader("Access-Control-Max-Age", "1800");//30 min
       // }
        filterChain.doFilter(request, response);
      }
    }

Please help me.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:44:59+00:00Added an answer on June 17, 2026 at 11:44 am

    Just try this.
    In your GWT code you are using FileUpload class.and the name is upload.
    so the GWT will generate an input field with name upload( check your html sources to confirm). so when you sumit form the browser send the field as param with same name.
    In your webservice you must catch the param with same name ..
    change your code like this

     public Response uploadFile(
                @FormDataParam("upload") InputStream uploadedInputStream,
                @FormDataParam("upload") FormDataContentDisposition fileDetail) {
    
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to upload a file using windows application to a web service so
I want to upload a file in chunks to a web service. // Web
Hi i want to upload mulitiple file using rails 3 and paperclip please help
I want to upload an Excel File using (HTML.Input) in some folder in server
I want to upload file to a host by using WebClient class. I also
I'm using Apache Commons FTP to upload a file. Before uploading I want to
I want to upload file using to a JSP page through android. If any
I want to upload a file on my aspx page. I am using <form
I'm using a form to upload a file using php. It works fine but
I want to upload a file from a jsp but my limitation is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.