SpringMvc上传图片,保存在服务器目录下

                 

                首先导入fileupload和commons-io包

                

 
          commons-fileupload
          commons-fileupload
          1.3.2
      
      
          commons-io
          commons-io
          2.4
      

        在Spring配置文件中配置

 

 


        
    

     前端页面的表单如下

  
选择文件
选择文件
选择文件

               控制器为

package com.imudges.controller;

/**
 * Created by Administrator on 2016/11/14.
 */


import com.imudges.model.CommodityEntity;
import com.imudges.model.ImageEntity;
import com.imudges.repository.CommodityRepository;
import com.imudges.repository.ImageRepository;
import com.imudges.utils.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
public class FileUploadController {

    //Logger logger = LoggerFactory.getLogger(FileUploadController.class);
    @Autowired
    CommodityRepository commodityRepository;
    @Autowired
    ImageRepository imageRepository;
    @RequestMapping("test")
    public String test(){
        return "test";
    }
    @RequestMapping(value = "addIndent",method = RequestMethod.POST)
    public String addIndent(String commodityName,MultipartFile imagetop,MultipartFile imagetwo,MultipartFile imagethree,int price,String description,double discount,HttpServletRequest request,ModelMap modelMap ) throws IOException {
        CommodityEntity commodityEntity = new CommodityEntity();
        ImageEntity imageEntity = new ImageEntity();
        String filePath1 = FileUpload.uploadFile(imagetop, request);
        String filePath2 = FileUpload.uploadFile(imagetwo, request);
        String filePath3 = FileUpload.uploadFile(imagethree, request);
        filePath1 = filePath1+";"+filePath2+";"+filePath3;
        imageEntity.setImg(filePath1);
        imageRepository.saveAndFlush(imageEntity);
        imageEntity =  imageRepository.findByImg(filePath1);
        commodityEntity.setCommodityname(commodityName);
        commodityEntity.setPrice(price);
        commodityEntity.setDescription(description);
        commodityEntity.setImageByImageId(imageEntity);
        commodityEntity.setDiscount(discount);
        commodityRepository.saveAndFlush(commodityEntity);
        List commodityEntities = commodityRepository.findAll();
        modelMap.addAttribute("commodityEntities",commodityEntities);
        return "cate";
    }

}

 

 

 

 

package com.imudges.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;


public class FileUpload {

    //文件上传
    public static String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
        String fileName = file.getOriginalFilename();
        String path=request.getSession().getServletContext().getRealPath("images/");
        File tempFile = new File(path, new Date().getTime() + String.valueOf(fileName));
        if (!tempFile.getParentFile().exists()) {
            tempFile.getParentFile().mkdir();
        }
        if (!tempFile.exists()) {
            tempFile.createNewFile();
        }
        file.transferTo(tempFile);
        return "images/" + tempFile.getName();
    }

}

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Java)