📄 fileuploadcontroller.java
字号:
package com.cib.webapp.controller;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.cib.Constants;import org.springframework.validation.BindException;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.multipart.commons.CommonsMultipartFile;import org.springframework.web.servlet.ModelAndView;/** * Controller class to upload Files. * * <p> * <a href="FileUploadFormController.java.html"><i>View Source</i></a> * </p> * * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> */public class FileUploadController extends BaseFormController { public FileUploadController() { setCommandName("fileUpload"); setCommandClass(FileUpload.class); } public ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (request.getParameter("cancel") != null) { return new ModelAndView(getCancelView()); } return super.processFormSubmission(request, response, command, errors); } public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { FileUpload fileUpload = (FileUpload) command; // validate a file was entered if (fileUpload.getFile().length == 0) { Object[] args = new Object[] { getText("uploadForm.file", request.getLocale()) }; errors.rejectValue("file", "errors.required", args, "File"); return showForm(request, response, errors); } MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file"); // the directory to upload to String uploadDir = getServletContext().getRealPath("/resources") + "/" + request.getRemoteUser() + "/"; // Create the directory if it doesn't exist File dirPath = new File(uploadDir); if (!dirPath.exists()) { dirPath.mkdirs(); } //retrieve the file data InputStream stream = file.getInputStream(); //write the file to the file specified OutputStream bos = new FileOutputStream(uploadDir + file.getOriginalFilename()); int bytesRead; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); //close the stream stream.close(); // place the data into the request for retrieval on next page request.setAttribute("friendlyName", fileUpload.getName()); request.setAttribute("fileName", file.getOriginalFilename()); request.setAttribute("contentType", file.getContentType()); request.setAttribute("size", file.getSize() + " bytes"); request.setAttribute("location", dirPath.getAbsolutePath() + Constants.FILE_SEP + file.getOriginalFilename()); String link = request.getContextPath() + "/resources" + "/" + request.getRemoteUser() + "/"; request.setAttribute("link", link + file.getOriginalFilename()); return new ModelAndView(getSuccessView()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -