Sign in

E-mail *, (xx@domain.com)
Password *

Register | Forgot password

Blogs

  • Bram de Kruijff
  • Ivo Ladage
  • Mark van Cuijk
  • Martin van Mierloo
  • Martijn van Berkum
  • Michel Teunissen
  • Patrick Atoon

Recent blogs

RSS - Blogs
March 9, 2010
State of OSGi in the Java world
March 4, 2010
Reach more people with Google Translate
March 3, 2010
Get My Advice
February 26, 2010
What? Where!?!
February 11, 2010
Split it!

All Blogs...


Files in and files out

March 13, 2008

The WebManager architecture allows panels with a user interface to be created very easily. However, when trying to generate a stream to the browser, I discovered nobody has done this the way I wanted and therefore I couldn’t rely on some existing documentation. Today I’ll write about streaming content to a browser from within a panel, streaming content from a browser back to the server and something about reading a ZIP file in Java.

Browser file uploads

Let’s start with the easy part: uploading a file from the browser to WebManager, using a WCB panel. There are only three pretty straight-forward things to do. First I define a MultipartFile property on the form-backing object, including a getter and setter. Second I place a file upload field in the panel, by adding this fragment to my edit JSP:

<wmedit:fileUpload path="command.importFile" />

At this point, after the WCB is built and deployed, an upload field is visible in the panel and when a file is selected, it is streamed to the server. The last thing we need to do is to process the file on the server, which is done inside the onSubmit() method in the tab controller. Via the form-backing object, the MultipartFile property can be accessed, which gives access to the file name, size, content type and the actual bytes. These bytes can be read into a byte array using getBytes() or streamed using the getInputStream() method.

On-the-fly ZIP file reading

So at this point I have an InputStream object to read the file the browser is sending. The intention of my panel is to upload a ZIP file, which the server should extract and process. One way to do this is to extract the ZIP file into a temporary directory, process the files from there and afterwards remove the temporary directory. However, Java provides a java.util.zip package, which provides ZipInputStream and ZipEntry for my code.  More information on reading and writing ZIP files with this package can be found in the JavaDoc.

try {
      zip = new ZipInputStream(stream);
      ZipEntry zipEntry = null;
      while ((zipEntry = zip.getNextEntry()) != null) {
            String filename = zipEntry.getName();
            log.finer("Processing entry " + filename);
      }
} finally {
      if (zip != null) {
            zip.close();
      }
}

Output file streaming

Now you have seen the easy way to upload files from a browser into a panel, I’ll show the easy way to stream content to the browser. On my panel is a button that says “Export” and a click on it will start a process that generates a CSV file which is streamed to the browser. What you already know is that submitting the panel will call the onSubmit() method on my tab controller. What the documentation does not say is that the behavior of WebManager after the onSubmit() method returns can be changed.

By default, WebManager will send a redirect to the browser after the onSubmit() method has finished, so the browser will request a new page. This happens because by default a redirect view is selected by WebManager. However, during onSubmit() this can be changed:

modelAndView.setViewName(null);
modelAndView.setView(new MyView());

The MyView class implements the org.springframework.web.servlet.View interface. After the onSubmit() method return and the submitted data is processed, the view will be used to render the data to send to the browser. So I’m in charge again and I’m in the position to stream my export:

class MyView implements View {
	public void render(Map controlMap, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String curDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());
		String filename = "export-" + curDate + ".csv";
		response.addHeader("Content-Disposition", "attachment;filename=" + filename);
		PrintWriter pw = new PrintWriter(response.getOutputStream());
		for (Entity entity: MyTabController.this.MyService.getAllEntities()) {
			// Write one line to the file
		}
		pw.close();
	}
	public String getContentType() {
		return "application/octet-stream";
	}
}

About the Author

Return to all blogs


Mark is software engineer with a special interest in Security and Digital WebTV. Mark writes about daily engineering with GX WebManager

Read all Marks blog entries

Other blog entries:

March 3, 2010
Get My Advice
February 11, 2010
Split it!
April 22, 2009
What goes in, must come out!
July 29, 2008
WCB Sharing FTW!
June 17, 2008
Found me on LinkedIn?
April 18, 2008
Tosti
April 7, 2008
Baking apple pie at 347
March 21, 2008
Short story about not inventing another wheel
February 29, 2008
Big Brother is watching


Share:

del.icio.us
digg
Technorati
Slashdot
Reddit
YahooMyWeb
NewsVine
ekudos
© 2010 GX creative online development B.V.

Disclaimer

This website (GXdeveloperweb.com) may discuss or contain opinions, (sample) coding, software or other information that does not include GX official interfaces, instructions or guidelines and therefore is not supported by GX. Changes made based on this information are not supported.  GX will not be held liable for any damages caused by using or misusing the information, software, instructions, code or methods suggested on this website, and anyone using these methods does so at his/her own risk. GX offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this website, including any liability resulting from incompatibility between the content of this website and the materials and services offered by GX. By using this website you will not hold, or seek to hold, GX responsible or liable with respect to the content of this website.