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

                 

                首先导入fileupload和commons-io包

                

 <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.2</version>
      </dependency>
      <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
      </dependency>
        在Spring配置文件中配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5242880"/>
    </bean>
     前端页面的表单如下
  <form method="post" class="form-x" action="addIndent" enctype="multipart/form-data">
            <div class="form-group">
                <div class="label">
                    <label>商品名称:</label>
                </div>
                <div class="field">
                    <input type="text" class="input" name="commodityName" value="" />
                    <div class="tips"></div>
                </div>
            </div>

            <div class="form-group">
                <div class="label">
                    <label>商品首页图片:</label>
                </div>
                <a href="javascript:;" class="file">
                    选择文件
                    <input id="imagetop" type="file" name="imagetop" onchange="change(imagetop);">
                </a>
            </div>

            <div class="form-group">
                <div class="label">
                    <label>商品图片1:</label>
                </div>
                <a href="javascript:;" class="file">
                    选择文件
                    <input type="file" name="imagetwo" >
                </a>
            </div>

            <div class="form-group">
                <div class="label">
                    <label>商品图片2:</label>
                </div>
                <a href="javascript:;" class="file">
                    选择文件
                    <input type="file" name="imagethree" >
                </a>
            </div>

            <div class="form-group">
                <div class="label">
                    <label>商品价格:</label>
                </div>
                <div class="field">
                    <input type="text" class="input" name="price" value="" />
                </div>
            </div>

            <div class="form-group">
                <div class="label">
                    <label>商品描述:</label>
                </div>
                <div class="field">
                    <textarea  class="input" name="description" style="height:80px"></textarea>
                    <div class="tips"></div>
                </div>
            </div>

            <div class="form-group">
                <div class="label">
                    <label>商品折扣:</label>
                </div>
                <div class="field">
                    <input type="text" class="input" name="discount" value="" />
                    <div class="tips"></div>
                </div>
            </div>

            <div class="form-group">
                <div class="label">
                    <label></label>
                </div>
                <div class="field">
                    <button class="button bg-main icon-check-square-o" type="submit"> 提交</button>
                </div>
            </div>
        </form>
               控制器为
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<CommodityEntity> 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();
    }

}





阅读更多
换一批

没有更多推荐了,返回首页