fileformcontroller.java
来自「Java的框架」· Java 代码 · 共 226 行
JAVA
226 行
package mcaps.core.docman.webapp.controller;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import mcaps.core.docman.util.DocManUtil;
import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.docman.model.FileObject;
import mcap.core.docman.service.DocAccessException;
import mcap.core.docman.service.DocExistsException;
import mcap.core.docman.service.DocManManager;
import mcap.core.docman.service.DocPathNotFoundException;
/**
* This class extends BaseFormController that will interacts with the
* DocManManager to handle request to retrieve/persist file info
* to data store.
*
* @author Chan Chin Wei
* @date Apr 12, 2006
* @version 1.0.1.0
*/
public class FileFormController extends BaseFormController {
private DocManManager docManager;
public DocManManager getDocManager() {
return docManager;
}
public void setDocManager(DocManManager docManager) {
this.docManager = docManager;
}
//===========================================================================================================
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.AbstractFormController#processFormSubmission(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
* Checks for an action that should be executed without respect to binding errors, like a cancel action.
*/
public ModelAndView processFormSubmission (HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
if (request.getParameter ("cancel") != null) {
return new ModelAndView (new RedirectView ("docMan.action?path="+DocManUtil.urlEncodePath (((FileObject) command).getParentId())));
}
return super.processFormSubmission (request, response, command, errors);
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
* Next to be called if processFormSubmission method called the
* super.processFormSubmission
*/
public ModelAndView onSubmit (HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Locale locale = request.getLocale();
FileObject file = (FileObject) command;
//remove file
if (request.getParameter ("delete") != null) {
try {
docManager.removeFile(file.getFilePath());
saveMessage (request, getText ("fileObject.deleted", file.getName(), locale));
return new ModelAndView (new RedirectView ("docMan.action?path=" + DocManUtil.urlEncodePath (file.getParentId())));
} catch (DocAccessException e) {
e.printStackTrace();
if (e instanceof DocPathNotFoundException)
errors.reject("errors.fileObject.deleteNotFound", new Object[] {file.getName ()}, "Unable to delete");
else
errors.reject ("errors.fileObject.deleteFail", new Object[] {file.getName ()}, "Unable to delete");
return showForm (request, response, errors);
}
}
//add file
else {
InputStream ips = null;
try {
String name = file.getName();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile commonFile = (CommonsMultipartFile) multipartRequest.getFile ("uploadFile");
if (commonFile != null) {
ips = commonFile.getInputStream();
file.setInputStream(ips);
}
file.setCreationDate(Calendar.getInstance());
file.setModificationDate(Calendar.getInstance());
if (name != null && !name.equals("")) {
int eindex = name.lastIndexOf(".");
//check if name is tag with the file ext.
String oriName = "";
if (commonFile != null)
oriName = commonFile.getOriginalFilename();
int oeindex = oriName.lastIndexOf(".");
if (eindex == -1) {
if (oeindex != -1) {
file.setName(name + oriName.substring(oeindex));
}
else if (file.getFilePath() != null && !file.getFilePath().equals("")) {
String oldname = file.getFilePath();
oeindex = oldname.lastIndexOf(".");
if (oeindex != -1)
file.setName(name + oldname.substring(oeindex));
}
}
/*
//append file ext if name is tag with different ext
else {
String ext = name.substring(eindex);
if (oeindex != -1 && !ext.equalsIgnoreCase(oriName.substring(oeindex))) {
file.setName(name + oriName.substring(oeindex));
}
}
*/
}
//name is empty, hence populate with upload file name.
else if (commonFile != null) {
//there is a limitation using current version of the Spring CommonsMultipartFile
//the function getOriginalFilename returned the name all in lowercase.
file.setName(commonFile.getOriginalFilename());
}
if (name.lastIndexOf(".") != -1)
file.setExt(name.substring (name.lastIndexOf(".") + 1));
if (commonFile != null)
file.setSize(commonFile.getSize());
String username = request.getRemoteUser();
file.setCreator(username);
file.setModifier(username);
docManager.saveFile(file);
saveMessage (request, getText ("fileObject.saved", file.getName(), locale));
return new ModelAndView (new RedirectView ("docMan.action?path=" + DocManUtil.urlEncodePath (file.getParentId())));
}
catch (DocAccessException e) {
e.printStackTrace();
if (e instanceof DocExistsException)
errors.reject("errors.fileObject.saveFailExist", new Object[] {file.getName ()}, "Unable to save");
else
errors.reject("errors.fileObject.saveFail", new Object[] {file.getName ()}, "Unable to save");
return showForm (request, response, errors);
}
finally {
try {
if (ips != null)
ips.close ();
}
catch (Exception e) {
//catch ioexception
}
}
}
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.AbstractFormController#showForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.validation.BindException)
* Calling in case of validation errors, to show the form view again.
*/
protected ModelAndView showForm (HttpServletRequest request,
HttpServletResponse response, BindException errors) throws Exception {
// prevent ordinary users from calling a GET on fileForm.html
// unless a bind error exists.
if ((request.getRequestURI ().indexOf ("fileForm") > -1)
&& (!request.isUserInRole (mcap.core.user.util.NameConstants.ADMIN_ROLE)
&& (errors.getErrorCount () == 0) &&
(request.getRemoteUser () != null))) {
response.sendError (HttpServletResponse.SC_FORBIDDEN);
return null;
}
return super.showForm (request, response, errors);
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
* Retrieve a backing object for the current form from the given request.
*/
protected Object formBackingObject (HttpServletRequest request)
throws Exception {
FileObject file = null;
String path = request.getParameter("path");
if (StringUtils.equals (request.getParameter ("method"), "add")) {
file = new FileObject();
file.setParentId(path);
}else{
file = docManager.getFile(path);
}
return file;
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)
* Set up a custom property editor for converting form inputs to real objects
*/
protected void initBinder (HttpServletRequest request,
ServletRequestDataBinder binder) {
// super.initBinder(request,binder);
SimpleDateFormat dateFormat = new SimpleDateFormat (getText ("date.format",
request.getLocale ()));
dateFormat.setLenient (false);
binder.registerCustomEditor (Date.class, null, new CustomDateEditor (
dateFormat, true));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?