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

📄 fileuploadcontroller.java

📁 Spring2.0宝典
💻 JAVA
字号:
package lee;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
import org.springframework.web.servlet.*;

import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;

/**
 * @author  yeeku.H.lee kongyeeku@163.com
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */
public class FileUploadController extends SimpleFormController
{

    protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,
        Object command, BindException errors)throws ServletException, IOException
    {        
        //强制类型转换
        FileUploadBean bean = (FileUploadBean)command;
        
        //获取文件内容
        byte[] file = bean.getFile();
        if (file == null)
        {
            System.out.println("上传的文件为空");
            return null;
        }
        else
        {
		    String realPath = getServletContext().getRealPath("/upfiles") + "/";
            String fileName = String.valueOf(System.currentTimeMillis());
			FileOutputStream out = new FileOutputStream(realPath + fileName);
			System.out.println("文件开始上传....");
			out.write(file);
			System.out.println("文件上传成功....");
			out.close();
            Map myModel = new HashMap();
            myModel.put("hint" , "上传成功");
            return new ModelAndView(getSuccessView(),"model",myModel);
        }

    }
    
    protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)
        throws ServletException 
    {
        // 为了完成将Multipart实例转换成字节数组,必须使用自定义属性编辑器
        // 此处使用ByteArrayMultipartEditor属性编辑器
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    }        
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -