Develop Spring MVC Apps with JBuilder2005 - Multipart (FileUpload)
Gaoke Hua
About the author: Gao Jinghua, Nanjing Aviation College computing a master's degree in mathematics, more than ten years of corporate informationization work experience. Current research interest, J2EE enterprise application, ERP software research and development, data warehouse system research and development.
1. Establish basic Spring applications according to "Developing Spring MVC Applications with JBuilder2005"
2. Add a Commons-FileUpload.jar class library
3. Add two class files
/ src / fileupload / fileuploadController.java
Package fileupload;
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Import org.springframework.validation. *;
Import org.springframework.Web.bind. *;
Import org.springframework.web.multipart.support. *;
Import org.springframework.web.servlet. *;
Import org.springframework.Web.Servlet.mvc. *;
// snippet from FileUploadController
Public Class FileUploadController Extends SimpleFormController {
Protected ModlandView OnSubmit
HTTPSERVLETREQUEST REQUEST,
HTTPSERVLETRESPONSE RESPONSE,
Object Command,
Bindexception error) throws servletexception, ioException {
// Cast the bean
FileUploadBean bean = (fileuploadbean) Command;
// let's see if there's content there
Byte [] file = bean.getfile ();
IF (file.length == 0) {
// HMM, That's Strange, The User Did Not Upload Anything
Return New ModelandView (this.GetformView ());
}
// Well, Let's do Nothing with the bean for now and return:
File myfile = new file ("myfile.txt");
myfile.createnewfile ();
Try {
Return Super.onSubmit (Request, Response, Command, Errors);
} catch (exception ex) {
Return NULL;
}
}
protected void initbinder
HTTPSERVLETREQUEST REQUEST,
ServletRequestDataBinder Binder) THROWS servletexception {
// TO ACTUALLY Be Able To Convert Multipart Instance To Byte []
// we have to register a Custom Editor (in this Case THE
// ByteArraymultiparteditor
Binder.registerCustomeditor (byte []. Class, New ByteArraymultipartFileEditor ());
// Now Spring Knows How To Handle Multipart Object And Convert THEM
}
}
If you have selected the file you want to upload, file.length is not 0 when you upload files, otherwise file.length is 0. The content of the upload file is saved in the Byte [] file, this example does not process the uploaded file.
/Web-inf/fileupload/fileuploadbean.java
Package fileupload;
// snippet from FileUploadBean
Public clas fileuploadbean {
PRIVATE BYTE [] File;
Public void setfile (Byte [] file) {
THIS.FILE = file;
}
Public Byte [] getFile () {
Return file;
}
}
4. The springApp-servlet.xml file is as follows:
/Web-inf/springapp-servlet.xml
XML Version = "1.0" encoding = "UTF-8"?>
- Application Context Definition for "SpringApp" DispatcherServlet.
->
prOPS>
Property>
bean>
Property>
proty>
Property>
bean>
Property>
Property>
Property>
bean>
"- one of the profmenties available; the maximum file size in bytes ->
Property>
bean>
beans>
The value of the attribute success confirmation refers to Confirmation.jsp. InternalResourceViewResolver allows us to omit prefix (file path) and suffix (.jsp)
5. File confirmation.jsp is the confirmation page after the successful file success
/Web-inf/jsp/confirmation.jsp
<% @ Page ContentType = "Text / HTML; Charset = BIG5"%>
SuccessView
title>
hEAD>
JBuilder Generated JSP
SuccessView
h1>
body>
html>
6. If the user does not select the file that is uploaded, submit the form, file fileuploadform.jsp prompts the user to select the file uploaded file and then submit
/Web-inf/jsp/fileuploadform.jsp
hEAD>