SpringMVC 上传文件<8>

1.在spring配置文件中添加

  
    

        
        
        
    
  1. Html5FileUploadController.java
package com.company.combine.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.company.combine.model.Product;
import com.company.combine.model.UploadFile;
import org.apache.commons.logging.Log;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;


@Controller
public class Html5FileUploadController {

    private static final Log logger = org.apache.commons.logging.LogFactory.getLog(Html5FileUploadController.class);

    @RequestMapping(value = "/html5")
    public String inputProduct(Model model) {
        System.out.println("----------inputProduct------------:");
        model.addAttribute("product", new Product());
        return "html5";
    }

    @RequestMapping(value = "/file_upload", method = RequestMethod.POST)
    public void saveFile(HttpServletRequest httpServletRequest, @ModelAttribute UploadFile uploadFile,
            BindingResult bindingResult, Model model)  {
        System.out.println("----------saveFile------------:");
        MultipartFile multipartFile = uploadFile.getMultipartFile();
        
        String fileName = multipartFile.getOriginalFilename();
//      String fileNames =fileName.substring(fileName.lastIndexOf("\\"));
        
        // File imageFile = new
        // File(httpServletRequest.getContextPath()+"/image",fileName);
        File imageFile = new File(httpServletRequest.getServletContext().getRealPath("/image"),fileName);

        String path = httpServletRequest.getServletContext().getRealPath("/image");
        if (!new File(path).exists()) {
            new File(path).mkdir();
        }
        System.out.println("fileName:"+fileName);
        System.out.println("multipartFile.getName():"+multipartFile.getName());
        System.out.println("path:"+path);
        System.out.println("imageFile.getAbsolutePath():"+imageFile.getAbsolutePath());
        
        try {
            multipartFile.transferTo(imageFile);
//          model.addAttribute("image", imageFile.getAbsolutePath().toString());
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

3.UploadFile.java

package com.company.combine.model;

import org.springframework.web.multipart.MultipartFile;

public class UploadFile {
    public MultipartFile getMultipartFile() {
        return multipartFile;
    }

    public void setMultipartFile(MultipartFile multipartFile) {
        this.multipartFile = multipartFile;
    }

    private MultipartFile multipartFile;
}

4.html5.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="GB2312"%>





登录


Multiple file uploads with progress bar



你可能感兴趣的:(SpringMVC 上传文件<8>)