📄 fileuploadcontroller.java
字号:
/*
* Created on 2006-10-23 12:33:04
*
* By SinoBest
* Copyright hnisi.com.cn, 2005-2006, All rights reserved.
*/
package cn.com.juneng.baseinfo.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.FileCopyUtils;
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.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import cn.com.juneng.baseinfo.service.BaseinfoService;
import cn.com.juneng.baseinfo.vo.BaseinfoVOImpl;
import cn.com.juneng.system.common.COMMON;
import cn.com.juneng.system.common.SpringBeanFactory;
/**
* @author yehailong
*
*/
public class FileUploadController extends SimpleFormController {
private static Log log =
LogFactory.getLog(FileUploadController.class);
private String uploadDir;// 上传文件路径
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object cmd, BindException errors)
throws Exception {
byte[] bytes=null;
request.setCharacterEncoding("GBK");
BaseinfoVOImpl vo=null;
// cast to multipart file so we can get additional information
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("xp");
String ryid=multipartRequest.getParameter("id");
bytes=file.getBytes();
BaseinfoService service = (BaseinfoService)SpringBeanFactory.getBean("baseinfoService");
if(COMMON.isEmpty(ryid)){
vo=new BaseinfoVOImpl();
vo.setRid(cn.com.juneng.system.common.COMMON.getUUID());
vo.setXb("1");
vo.setXm("未录入");
vo.setXp(bytes);
service.create(vo);
}
else{
vo=service.findByPrimaryKey(ryid);
vo.setXp(bytes);
service.insertUpdate(vo);
}
vo.setActionType("upload");
request.setAttribute("VO", vo);
return new ModelAndView(getSuccessView() + ".jsp?rid="+ryid);
}
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
public void setUploadDir(String uploadDir){
this.uploadDir = uploadDir;
}
public String getUploadDir(){
return this.uploadDir;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -