FileUploadController

此博客展示了一个Java的文件上传控制器代码。该控制器基于Spring的SimpleFormController,使用Servlet处理文件上传。通过设置上传路径、绑定数据等操作,实现文件上传功能,并将上传文件路径存储在会话中,涉及Servlet、Java、Bean等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cn.com.employee.web.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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.employee.domain.FileUploadBean;

import sun.rmi.runtime.Log;

public class FileUploadController extends SimpleFormController {

private String uploadDir;// 上传文件路径

public FileUploadController() {

setCommandClass(FileUploadBean.class);
setCommandName("fileUploadBean");
setFormView("uploadForm");
}

protected Map referenceData(HttpServletRequest request) throws Exception {

Map model = new HashMap();
model.put("filePathList", request.getSession().getAttribute(
"filePathList"));
// model.put("categories", this.petStore.getCategoryList());
return model;
}

protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object cmd, BindException errors)
throws Exception {

List list = new ArrayList();
HttpSession httpSession = request.getSession();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
.getFile("file");

String upLoadDir = this.getUploadDir();
File dirPath = new File(uploadDir);

String sep = System.getProperty("file.separator");

String filePath = upLoadDir + "/" + file.getOriginalFilename();

if (null != request.getSession().getAttribute("filePathList")) {
list = (ArrayList) request.getSession()
.getAttribute("filePathList");
list.add(filePath);

} else {
list.add(filePath);
}
httpSession.setAttribute("filePathList", list);

System.out.println("上传文件路径" + upLoadDir + "/"
+ file.getOriginalFilename());

return new ModelAndView("uploadForm");

/*
* FileUploadBean bean = (FileUploadBean) cmd; byte[] bytes =
* bean.getFile();
*
* MultipartHttpServletRequest multipartRequest =
* (MultipartHttpServletRequest) request; CommonsMultipartFile file =
* (CommonsMultipartFile) multipartRequest .getFile("file");
*
* String uploadDir = this.getUploadDir();
*
* File dirPath = new File(uploadDir);
*
* if (!dirPath.exists()) { dirPath.mkdirs(); } String sep =
* System.getProperty("file.separator");
*
* File uploadedFile = new File(uploadDir + sep +
* file.getOriginalFilename()); FileCopyUtils.copy(bytes, uploadedFile);
*
*
* return new ModelAndView(getSuccessView());
*/
}

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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值