GWT文件上传到REST Web服务

时间:2013-01-11 05:55:35

标签: rest gwt

我想使用GWT将文件上传到REST Web服务。但是我在以下这些字段中得到了空值。

InputStream uploadedInputStream, FormDataContentDisposition fileDetail

以下是我的GWT客户端代码

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());

            }
        });
    }
}

以下是我的服务器端代码。我在客户端和服务器之间建立了连接,但只获得了空值。我不明白我的错误。我引用了很多网站,但我得到了相同的代码。

@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);
      }
    }

请帮帮我。 提前谢谢。

3 个答案:

答案 0 :(得分:1)

试试吧。 在您使用GWT类的FileUpload代码中,名称为upload。 所以GWT将生成一个名为upload 的输入字段(检查您的html来源以确认)。因此,当您汇总表格时,浏览器会将该字段作为具有相同名称的参数发送。 在您的Web服务中,您必须捕获具有相同名称的参数。 像这样改变你的代码

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


 }

答案 1 :(得分:0)

您是否尝试过基于事件的方法,例如this

答案 2 :(得分:0)

您必须创建一个servlet,并创建servlet映射。有关如何与服务器通信的完整详细信息,请参阅以下链接

https://developers.google.com/web-toolkit/doc/1.6/DevGuideServerCommunication