|
File: UploadController.java. This servlet performs a Spring Upload and contains the code to do this operation.
package org.springframework.web.portlet.sample;
import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import javax.portlet.PortletRequest; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse;
import org.springframework.validation.BindException; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; import org.springframework.web.portlet.ModelAndView; import org.springframework.web.portlet.bind.PortletRequestDataBinder; import org.springframework.web.portlet.multipart.MultipartActionRequest; import org.springframework.web.portlet.mvc.SimpleFormController;
public class UploadController extends SimpleFormController {
public void onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception {
if (request instanceof MultipartActionRequest) { MultipartActionRequest multipartRequest = (MultipartActionRequest) request; MultipartFile multipartFile = multipartRequest.getFile("file"); if (multipartFile != null) { logger.info("isEmpty: " + multipartFile.isEmpty()); logger.info("contentType: " + multipartFile.getContentType()); logger.info("name: " + multipartFile.getName()); logger.info("size: " + multipartFile.getSize()); logger.info("originalFilename: " + multipartFile.getOriginalFilename()); if (!"text/plain".equals(multipartFile.getContentType())) { throw new PortletException("File is of type '" + multipartFile.getContentType() + "', not 'text/plain'"); } logger.info("content: " + multipartFile.getBytes().toString()); } else { logger.info("MultipartFile returned NULL"); } }
Upload upload = (Upload)command; if (upload != null) { byte[] file = upload.getFile(); if (file != null) { String uploadString = new String(file); response.setRenderParameter("upload",uploadString); } } }
protected ModelAndView onSubmitRender(RenderRequest request, RenderResponse response, Object command, BindException errors) throws Exception { return this.showNewForm(request, response); }
protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); //binder.registerCustomEditor(String.class, new StringMultipartFileEditor()); }
}
This file provides an interface to the upload operation
package org.springframework.web.portlet.sample;
import java.io.Serializable;
public class Upload implements Serializable {
private static final long serialVersionUID = 1L;
private byte[] file;
public Upload() { super(); }
public Upload(byte[] file) { super(); setFile(file); }
public void setFile(byte[] file) { this.file = file; }
public byte[] getFile() { return file; }
}
Here is the jsp which makes the call to the upload code.
<%@ include file="/WEB-INF/jsp/include.jsp" %> <portlet:defineObjects/>
<h1>File Upload</h1>
<p> Please upload a small text file (under 2KB) and the contents will be displayed back to you.</p>
<form method="post" action="<portlet:actionURL/>" enctype="multipart/form-data"> <input type="file" name="file"/><br/> <button type="submit">Go</button> </form>
<p>Content:</p> <pre>${fn:escapeXml(param["upload"])}</pre>
|