⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadaction.java

📁 自己编写的一个spring框架下实现文件上传的例子
💻 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 + -