📄 uploadaction.java
字号:
package com.mr.UpLoad;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class UpLoadAction extends SimpleFormController {
private String storePath;
private UpLoadDao upLoadDao;
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object obj,BindException bind) throws Exception{
FileForm form=(FileForm)obj;
String path=storePath+System.getProperty("file.separator")+form.getName();
double number=(double)form.getContents().length/1024;
DecimalFormat df=new DecimalFormat("0.00");
String size=df.format(number);
upLoadDao.getJtl().update("insert into tab_image (tpname,path,tpsize,contents) values (?,?,?,?)", new Object[]{form.getName(),path,size,form.getContents()});
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(path));
bufferedOutputStream.write(form.getContents());
bufferedOutputStream.flush();
bufferedOutputStream.close();
return new ModelAndView(getSuccessView(),"filename",form.getName());
}
public String getStorePath() {
return storePath;
}
public void setStorePath(String storePath) {
this.storePath = storePath;
}
public UpLoadDao getUpLoadDao() {
return upLoadDao;
}
public void setUpLoadDao(UpLoadDao upLoadDao) {
this.upLoadDao = upLoadDao;
}
public UpLoadAction() {
setCommandClass(FileForm.class);
}
protected void initBinder(HttpServletRequest req0,
ServletRequestDataBinder binder) throws Exception {
super.initBinder(req0, binder);
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -