|
|
|
Spring3 Simple file upload 3 |
4: /classes/com/company/springapp/UploadCommand
package com.company.springapp;
import java.io.Serializable;
import org.springframework.web.multipart.MultipartFile;
public class UploadCommand implements Serializable
{
private MultipartFile document1;
private MultipartFile document2;
private MultipartFile document3;
private MultipartFile document4;
public MultipartFile getDocument1() {
return document1;
}
public void setDocument1(MultipartFile document1) {
this.document1 = document1;
}
public MultipartFile getDocument2() {
return document2;
}
public void setDocument2(MultipartFile document2) {
this.document2 = document2;
}
public MultipartFile getDocument3() {
return document3;
}
public void setDocument3(MultipartFile document3) {
this.document3 = document3;
}
public MultipartFile getDocument4() {
return document4;
}
public void setDocument4(MultipartFile document4) {
this.document4 = document4;
}
}
Note use of org.springframework.web.multipart.MultipartFile
5: /classes/com/company/springapp/UploadController
package com.company.springapp;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = "/upload")
public class UploadController {
private static final Logger logger = Logger
.getLogger(UploadController.class);
private String uploadFolderPath;
public String getUploadFolderPath() {
return uploadFolderPath;
}
public void setUploadFolderPath(String uploadFolderPath) {
logger.info(uploadFolderPath);
this.uploadFolderPath = uploadFolderPath;
}
@RequestMapping(value = "/form")
protected ModelAndView loginformHandler(@ModelAttribute("command")
UploadCommand command) throws Exception {
logger.info("start");
ModelAndView mav = new ModelAndView();
try {
mav.setViewName("upload");
mav.addObject(command);
} catch (Exception e) {
e.printStackTrace();
}
return mav;
}
@RequestMapping(value = "/save")
protected ModelAndView saveHandler(@ModelAttribute("command")
UploadCommand command) throws Exception {
logger.info("start::" + BeanUtils.describe(command));
ModelAndView mav = new ModelAndView();
try {
writeFile((UploadCommand) command);
mav.setViewName("uploadSuccess");
} catch (Exception e) {
e.printStackTrace();
}
return mav;
}
private void writeFile(UploadCommand bean) throws Exception {
String filePath = getUploadFolderPath() + getDateyyyyMMdd_HHmmssSS();
try {
logger.info("Start file write");
if (bean.getDocument1() != null
&& bean.getDocument1().getSize() > 0) {
bean.getDocument1().transferTo(
new File(filePath
+ "_1"
+ getFileExt(bean.getDocument1()
.getOriginalFilename())));
}
if (bean.getDocument2() != null
&& bean.getDocument2().getSize() > 0) {
bean.getDocument2().transferTo(
new File(filePath
+ "_2"
+ getFileExt(bean.getDocument2()
.getOriginalFilename())));
}
if (bean.getDocument3() != null
&& bean.getDocument3().getSize() > 0) {
bean.getDocument3().transferTo(
new File(filePath
+ "_3"
+ getFileExt(bean.getDocument3()
.getOriginalFilename())));
}
if (bean.getDocument4() != null
&& bean.getDocument4().getSize() > 0) {
bean.getDocument4().transferTo(
new File(filePath
+ "_4"
+ getFileExt(bean.getDocument4()
.getOriginalFilename())));
}
logger.info("End file write");
} catch (Exception e) {
logger.error(e, e);
}
}
private static String getDateyyyyMMdd_HHmmssSS() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSS");
return sdf.format(new Date(System.currentTimeMillis()));
}
private static String getFileExt(String filePath) {
return filePath.substring(filePath.lastIndexOf("."), filePath.length());
}
}
Note use of MultipartFile::transferTo() function
see @Controller, @RequestMapping(value="/upload"), @RequestMapping(value="/form"), @RequestMapping(value="/save")
6: /WEB-INF/jsp/upload.jsp
<%
String ctx = request.getContextPath();
%>
<html>
<head>
<title>/WEB-INF/jsp/upload.jsp</title>
</head>
<body>
<form name="uploadDocs" method="post" action="<%=ctx%>/upload/save"
enctype="multipart/form-data">
<table width="100%" border="0" cellpadding="0" cellspacing="0"
bgcolor="#FFFFFF">
<tr>
<td><input type="file" value="Upload Docs1" name="document1" />
</td>
</tr>
<tr>
<td><input type="file" value="Upload Docs2" name="document2" />
</td>
</tr>
<tr>
<td><input type="file" value="Upload Docs3" name="document3" />
</td>
</tr>
<tr>
<td><input type="file" value="Upload Docs4" name="document4" />
</td>
</tr>
<tr>
<td><input type="submit" value="UploadDocs" name="uploaddocs" />
</td>
</tr>
</table>
</form>
</body>
</html>
Note enctype="multipart/form-data"
6: /WEB-INF/jsp/uploadSuccess.jsp
<%
String ctx = request.getContextPath();
%>
<html>
<head>
<title>/WEB-INF/jsp/uploadSuccess.jsp</title>
</head>
<body onload="">
<form name="upload" method="post" action="upload">
<table width="100%" border="0" cellpadding="0" cellspacing="0"
bgcolor="#FFFFFF">
<tr>
<td>Upload Successful</td>
</tr>
</table>
</form>
</body>
</html>
|
|
|
| How to loop arraylist JAVA6 Syntax
|
| How to iterrate through ArrayList and display multiple rows using for loop JAVA6 syntax..
|
| Java JSP |
2010-Oct-12 |
| How to loop arraylist
|
| How to iterrate through ArrayList and display multiple rows using for loop, JSTL tags..
|
| Java JSP |
2010-Oct-12 |
| Consumer using JAX WS Dispatch API and DOM parser 1
|
| Access/Consume Webservice using Servlet, JAX-WS Dispatch API dynamic client and parsing SOAP response using DOM parser...
|
| Java Webservice |
2010-Sep-20 |
| Simple Java first CXF Webservice 4
|
| Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
|
| Java Webservice |
2010-Sep-19 |
| Simple Java first CXF Webservice 3
|
| Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
|
| Java Webservice |
2010-Sep-18 |
| Simple Java first CXF Webservice 1
|
| Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
|
| Java Webservice |
2010-Sep-18 |
| Simple Java first CXF Webservice 2
|
| Building Simple Java first CXF webservice using CXFServlet, Spring, jaxws:endpoint, @WebService, @SOAPBinding, @WebResult
|
| Java Webservice |
2010-Sep-18 |
| Encoding special characters in userinput or on server
|
| Encoding can be done either in Javascript or JAVA encodeURIComponent, escape, java.net.URLEncoder..
|
| Java JSP |
2010-Aug-09 |
| Jstl fmt tag i18n formatdate formatcurrency
|
| fmt:setBundle, native2ascii.exe, fmt:message, fmt:setLocale, fmt:formatDate, fmt:formatNumber type=currency..
|
| Java JSP |
2010-Aug-05 |
| How to read and write a file
|
| How to read and write a file java.io.BufferedReader,java.io.FileReader, InputStreamReader ...
|
| Java J2SE |
2010-Aug-04 |
| jQuery validate form using ajax 2
|
| How to validate/submit form using ajax and jQuery input#, ($.ajax)..
|
| Javascript |
2010-Jul-23 |
| jQuery validate form using ajax 1
|
| How to validate/submit form using ajax and jQuery ($.ajax)..
|
| Javascript |
2010-Jul-23 |
| How to create datasource in RAD Websphere
|
| Create JDBC provider, datasource and JAAS security setup, JDBC connection URLs
|
| Servers Websphere |
2010-Jul-08 |
| Get Started 4
|
| Struts2 framework structure, how various components fit together
|
| Java Struts2 |
2010-Jul-07 |
| How to populate a form when JSP is called first time
|
| This is achieved throught setting bean in request, using frameworks like Struts2, Spring3..
|
| Java JSP |
2010-Jul-07 |
| Spring3 And Hibernate 4
|
| Spring3 and Hibernate 3.5.3 working together @Controller, @RequestMapping, @InitBinder, HibernateTransactionManager, LocalSessionFactoryBean, HibernateTemplate
|
| Java Spring |
2010-Jul-07 |
|
|