Reputation: 2455
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.
Upvotes: 3
Views: 2292
Reputation: 1973
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) {
}
Upvotes: 1
Reputation: 3380
You have to create a servlet, and create servlet mapping. Refer the following link for complete details on how to communicate with server
https://developers.google.com/web-toolkit/doc/1.6/DevGuideServerCommunication
Upvotes: 0