gpt4 book ai didi

java - spring中如何从jsp插入文件?

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:34 25 4
gpt4 key购买 nike

我正在尝试将数据与图像文件一起插入。图像不是插入到数据上,而是插入到 WEB-INF/resources/images 中。当我单击“提交”按钮时,没有插入任何内容。

<form class="form-horizontal" method="post" action="<c:url 
value="/admin/addProduct"/>" enctype = "multipart/form-data">
<div class="form-group">
<label for="productCode" class="col-sm-2 control-label">Code</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="productCode" name="productCode"
placeholder="product code">
</div>
</div>

<div class="form-group">
<label for="productName" class="col-sm-2 control-label">Product Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="productName" name="productName"
placeholder="product name">
</div>
</div>
<div class="form-group">
<label for="productPrice" class="col-sm-2 control-label">Price</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="productPrice" name="price"
placeholder="product price">
</div>
</div>
<div class="form-group">
<label>Upload Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
<input type="file" id="imgInp" name="prodImage">
</span>
</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Add Product</button>
</div>
</div>
</form>

产品 Controller

@RequestMapping(value="/admin/addProduct", method= RequestMethod.POST)
public String addNewProduct(@ModelAttribute("product")
Product products,BindingResult result,HttpServletRequest request) {
System.out.println("adding product");

if(result.hasErrors()) {
return "addProduct";
}
productService.saveProduct(products);

MultipartFile productImage = products.getImage();
String rootDir = request.getSession().getServletContext().getRealPath("/");
path = Paths.get(rootDir+"//WEB-INF//resources//images//"+products.getId()+".png");
if(productImage != null && !productImage.isEmpty()) {
System.out.println("inside not null product image");
try {
productImage.transferTo(new File(path.toString()));
}catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("product image saving failed",ex);
}
}

return "redirect:/admin/productInventory";
}

我的 pom.xml 看起来像:-

我的 pom 包含 commons-fileupload 和 commons-io。

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itn</groupId>
<artifactId>ecommerce</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ecommerce Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<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.5</version>
</dependency>

<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">

<bean class="org.springframework.beans.factory.config.
PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/db.properties</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.
LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.
MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.itn</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.
HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.
DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.
commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1024000" />
</bean>
</beans>

当我忽略 enctype = "multipart/form-data" 时,我可以成功插入表单数据(没有文件),但是每当我在表单中包含多部分并在 Debug模式下运行时,不会出现任何内容。我在这里缺少什么?

最佳答案

也许您需要连接一个多部分解析器:

@Bean(name = "multipartResolver")
public CommonsMultipartResolver createMultipartResolver() {
CommonsMultipartResolver resolver=new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxUploadSize(1000000);
return resolver;
}

关于java - spring中如何从jsp插入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45621137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com