无法从Spring Mvc上传和读取图像

时间:2018-02-10 17:06:12

标签: java spring jsp spring-mvc tomcat

我正在使用spring mvc构建销售网站,但是在上传照片和从资源文件夹中加载图片时遇到问题。我使用MultipartFile库将文件上传到WEB-INF / images的项目目录,但图像是没有上传。

项目结构

Project structure

EmusicController

package com.emusic.controller;

import com.emusic.dao.ProductDao;
import com.emusic.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
public class EmusicController {

    private Path path;

    @Autowired
    private ProductDao productDao;

   @RequestMapping(value = "/admin/productInventory/addProduct")
    public String addProduct(Model model, @ModelAttribute("product") Product product) {
        Product product1 = new Product();
        model.addAttribute("Product", product1);
        return "addProduct";
    }

    @RequestMapping(value = "/admin/productInventory/addProduct", method = RequestMethod.POST)
    public String addProductAdmin(@ModelAttribute("product") Product product, HttpServletRequest request) {

        MultipartFile productImage = product.getProductImages();
        String rootDirectory = request.getSession().getServletContext().getRealPath("/");
        path = Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\" + product.getProductId()+ ".png");

        if (productImage != null && !productImage.isEmpty()) {
            try {
                productImage.transferTo(new File(path.toString()));
            } catch (Exception e) {
                throw new RuntimeException("Can't save images\n" + e);
            }
        }
        productDao.createProduct(product);
        return "redirect:/admin/productInventory";
    }

}

产品

package com.emusic.model;


import org.springframework.web.multipart.MultipartFile;

import javax.persistence.*;

@Entity
@Table(name = "Product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    private Integer productId;
    private String productName;
    private String productCategory;
    private String productDescription;
    private double productPrice;
    private String productCondition;
    private double unitInStock;
    private String productManufacturer;
    private String productStatus;

    @Transient
    private MultipartFile productImages;

    public MultipartFile getProductImages() {
        return productImages;
    }

    public void setProductImages(MultipartFile productImages) {
        this.productImages = productImages;
    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductCategory() {
        return productCategory;
    }

    public void setProductCategory(String productCategory) {
        this.productCategory = productCategory;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductCondition() {
        return productCondition;
    }

    public void setProductCondition(String productCondition) {
        this.productCondition = productCondition;
    }

    public double getUnitInStock() {
        return unitInStock;
    }

    public void setUnitInStock(double unitInStock) {
        this.unitInStock = unitInStock;
    }

    public String getProductManufacturer() {
        return productManufacturer;
    }

    public void setProductManufacturer(String productManufacturer) {
        this.productManufacturer = productManufacturer;
    }

    public String getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(String productStatus) {
        this.productStatus = productStatus;
    }
}

Servlet的context.xml中

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.emusic" />
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1024000" />
    </bean>


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
    <tx:annotation-driven />
</beans>

AddProduct.jsp

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@include file="template/header.jsp" %>
<!-- Marketing messaging and featurettes
================================================== -->
<!-- Wrap the rest of the page in another container to center all the content. -->
<div class="container-wrapper">
    <div class="container">
        <div class="page-header">
            <h1>Add Product</h1>


            <p class="lead">This is page add all product</p>
        </div>

        <form:form method="post" commandName="product"
                   action="${pageContext.request.contextPath}/admin/productInventory/addProduct" enctype="multipart/form-data">
        <div class="form-group">
            <label for="name">Name</label>
            <form:input path="productName" id="name" class="form-Control"/>
        </div>
        <div class="form-group">
            <label for="category">Category:</label>
            <label class="check-box"><form:radiobutton path="productCategory" id="category"
                                                       value="instructerment"/>Instrucment</label>
            <label class="check-box"><form:radiobutton path="productCategory" id="category"
                                                       value="record"/>Record</label>
            <label class="check-box"><form:radiobutton path="productCategory" id="category"
                                                       value="accessory"/>Accesssory</label>
        </div>

        <div class="form-group">
            <label for="description">Description</label>
            <form:textarea path="productDescription" id="description" class="form-Control"/>
        </div>
        <div class="form-group">
            <label for="price">Price</label>
            <form:input path="productPrice" id="price" class="form-Control"/>
        </div>
        <div class="form-group">
            <label for="condition">Condition:</label>
            <label class="check-box"><form:radiobutton path="productCondition" id="condition"
                                                       value="new"/>New</label>
            <label class="check-box"><form:radiobutton path="productCondition" id="condition"
                                                       value="used"/>Used</label>
        </div>
        <div class="form-group">
            <label for="status">Status:</label>
            <label class="check-box"><form:radiobutton path="productStatus" id="status"
                                                       value="Active"/>Active</label>
            <label class="check-box"><form:radiobutton path="productStatus" id="status"
                                                       value="Inactive"/>Inactive</label>
        </div>
        <div class="form-group">
            <label for="unitInStock">Unit In Stock</label>
            <form:input path="unitInStock" id="unitInStock" class="form-Control"/>
        </div>
        <div class="form-group">
            <label for="manufacturer">ManuFacturer</label>
            <form:input path="productManufacturer" id="manufacturer" class="form-Control"/>
        </div>

        <div class="form-group">
            <label class="control-label" for="productImages">Upload Images</label>
            <br>
            <span>Picture Size: 700 x 500.</span>
            <br>
            <form:input path="productImages" id="productImages" type="file"  class="form:form-control" />
            <br>
        </div>

        <br><br>
        <input type="submit" value="Submit" class="btn btn-primary"/>
        <a href="<c:url value="/admin/productInventory/addProduct" />" class="btn btn-info">Cancel</a>

        </form:form>


        <%@include file="template/footer.jsp" %>

上传后,Spring上传到 E:\ Ebooktinhoc \ Java \ apache-tomcat-8.5.16-windows-x64 \ apache-tomcat-8.5.16 \ webapps \ ROOT \ WEB-INF \ resources \ images \ null.png 而不是我的项目。当我检查时,其中没有文件。为什么春天上传到它?如何将照片上传到资源/图像文件夹。请帮忙。谢谢

0 个答案:

没有答案