How a user can upload a file on the client side (Google Web Toolkit)

I use GWT (Google Web Toolkit) to create a website. I need to show the table to the user and allow the user to load the contents of the table.

On the client side, how can a user upload a file by clicking the download button?

The download button has an onClick() listener. And the client-side class extends Composite .

I tried to extend the HttpServlet class, but it gets too complicated.

I already read the posts here:

But I still don't know how I can provide a downloadable file to a client-side user.

+7
source share
4 answers

You really need to distinguish between java code on the client side of GWT and Java code on the server side.

Client-side in your Java GWT code

 String url = GWT.getModuleBaseURL() + "downloadService?fileInfo1=" + fileInfo1; Window.open( url, "_blank", "status=0,toolbar=0,menubar=0,location=0"); 

On the server side, in non-gwt Java code -

In web.xml

 <servlet> <servlet-name>downloadService</servlet-name> <servlet-class>AAA.BBB.CCC.DownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>downloadService</servlet-name> <url-pattern>/<gwtmodulename>/downloadService</url-pattern> </servlet-mapping> 

In servlet server package code

  public class DownloadServlet extends HttpServlet{ protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String fileName = req.getParameter( "fileInfo1" ); int BUFFER = 1024 * 100; resp.setContentType( "application/octet-stream" ); resp.setHeader( "Content-Disposition:", "attachment;filename=" + "\"" + fileName + "\"" ); ServletOutputStream outputStream = resp.getOutputStream(); resp.setContentLength( Long.valueOf( getfile(fileName).length() ).intValue() ); resp.setBufferSize( BUFFER ); //Your IO code goes here to create a file and set to outputStream// } } 

Make sure you click the contents of your file to **outputStream** .

+21
source

If you know the path to the file, the code snippet is shown below.

 button.addClickHandler(new ClickHandler() { @Overrid public void onClick(ClickEvent event) { Window.open(GWT.getHostPageBaseURL() + "/file.rar", "name", "enabled"); } }); 
+3
source

You can try ClientIO to read and write files on the client using GWT

http://www.emitrom.com/blog/client-io

0
source

To fulfill the answer of element number one in the io part ...

you can link to this link

http://www.programcreek.com/2009/02/java-convert-a-file-to-byte-array-then-convert-byte-array-to-a-file/

or refer to this code

 File file = new File(enter the filepath here) FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; try { for (int readNum; (readNum = fis.read(buf)) != -1;) { bos.write(buf, 0, readNum); //no doubt here is 0 //Writes len bytes from the specified byte array starting at offset off to this byte array output stream. System.out.println("read " + readNum + " bytes,"); } } catch (IOException ex) { System.err.println("unable to convert to bytes"); } byte[] bytes = bos.toByteArray(); outputStream.write(bytes); outputStream.flush(); outputStream.close(); 

hope this helps!

0
source

All Articles