GWT FileUpload - servlet parameters and response to processing

I am new to GWT and trying to implement a file upload function. I found some help with implementation over the Internet and used this as a link. But there are some questions related to this:

  • The actual loading or writing of the contents of the file on the server (or disk) will be performed by the servlet. Does this servlet (like MyFileUploadServlet) need to extend the HttpServlet? OR Can I use RemoteServiceServlet or implement any other interface? If so, which method do I need to implement / override?

  • In my servlet, after everything is done, I need to return the response back to the client. I think you can use form.addSubmitCompleteHandler () for this. From a servlet, I could return text / html (or an object of type String) and then use SubmitCompleteEvent.getResults () to get the result. The question is, can I use my custom object instead of String (say MyFileUploadResult), populate its results and pass it back to the client? or can i return a json object?

  • Currently, returning the response and using SubmitCompleteEvent.getResults (), I get some HTML tags added to the actual answer, such as:

pre> Image loading successfully / pre>.

Is there any way to get rid of this?

Many thanks!

Hi,

Ashish

+4
source share
1 answer

To upload files, in the past I expanded HttpServlet. I used it along with Commons-FileUpload .

I created a generic form-based download widget. This was to host downloads for different types of files (plain text and Base64). If you just need to download text files, you can combine the following two classes into one.

public class UploadFile extends Composite { @UiField FormPanel uploadForm; @UiField FileUpload fileUpload; @UiField Button uploadButton; interface Binder extends UiBinder<Widget, UploadFile> {} public UploadFile() { initWidget(GWT.<Binder> create(Binder.class).createAndBindUi(this)); fileUpload.setName("fileUpload"); uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART); uploadForm.setMethod(FormPanel.METHOD_POST); uploadForm.addSubmitHandler(new SubmitHandler() { @Override public void onSubmit(SubmitEvent event) { if ("".equals(fileUpload.getFilename())) { Window.alert("No file selected"); event.cancel(); } } }); uploadButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { uploadForm.submit(); } }); } public HandlerRegistration addCompletedCallback( final AsyncCallback<String> callback) { return uploadForm.addSubmitCompleteHandler(new SubmitCompleteHandler() { @Override public void onSubmitComplete(SubmitCompleteEvent event) { callback.onSuccess(event.getResults()); } }); } } 

The UiBinder part is pretty complicated.

 <g:HTMLPanel> <g:HorizontalPanel> <g:FormPanel ui:field="uploadForm"> <g:FileUpload ui:field="fileUpload"></g:FileUpload> </g:FormPanel> <g:Button ui:field="uploadButton">Upload File</g:Button> </g:HorizontalPanel> </g:HTMLPanel> 

Now you can extend this class for text files. Just make sure your web.xml serves for the HttpServlet in /textupload .

 public class UploadFileAsText extends UploadFile { public UploadFileAsText() { uploadForm.setAction(GWT.getModuleBaseURL() + "textupload"); } } 

The servlet for text files goes on the server side. It returns the contents of the downloaded file to the client. Be sure to install the jar for FileUpload from Apache Commons somewhere in your class path.

 public class TextFileUploadServiceImpl extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (! ServletFileUpload.isMultipartContent(request)) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Not a multipart request"); return; } ServletFileUpload upload = new ServletFileUpload(); // from Commons try { FileItemIterator iter = upload.getItemIterator(request); if (iter.hasNext()) { FileItemStream fileItem = iter.next(); // String name = fileItem.getFieldName(); // file name, if you need it ServletOutputStream out = response.getOutputStream(); response.setBufferSize(32768); int bufSize = response.getBufferSize(); byte[] buffer = new byte[bufSize]; InputStream in = fileItem.openStream(); BufferedInputStream bis = new BufferedInputStream(in, bufSize); long length = 0; int bytes; while ((bytes = bis.read(buffer, 0, bufSize)) >= 0) { out.write(buffer, 0, bytes); length += bytes; } response.setContentType("text/html"); response.setContentLength( (length > 0 && length <= Integer.MAX_VALUE) ? (int) length : 0); bis.close(); in.close(); out.flush(); out.close(); } } catch(Exception caught) { throw new RuntimeException(caught); } } } 

I can’t remember how I came across the <pre></pre> . You may need to filter tags on the client. The topic is also addressed here .

+2
source

Source: https://habr.com/ru/post/1401498/


All Articles