我正在尝试使用struts 2实现文件上传功能。但我得到错误" Source不能为空"。当我调试代码时,源(上传)文件数据似乎是空。
以下是我的代码,
add_categories.jsp
<div class="container">
<div class="row">
<section class="col-xs-12">
<form action="addCategory" class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="categoryName" class="col-sm-2 control-label">Category Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="categoryName" name="categoryName"/>
</div>
</div>
<div class="form-group">
<label for="categoryDescription" class="col-sm-2 control-label">Category Description</label>
<div class="col-sm-10">
<textarea id="categoryDescription" name="categoryDescription" class="form-control"></textarea>
</div>
</div>
<div class="form-group">
<label for="categoryImage" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<input type="file" id="categoryImage" name="categoryImage" class="form-control"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn btn-default" value="submit"/>
</div>
</div>
</form>
</section>
</div>
struts.xml中
?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="admin" namespace="/admin" extends="struts-default">
<action name="viewCategories" class="org.miraj.javabrains.actions.CategoryAction" method="viewCategories">
<result name="success">/view_categories.jsp</result>
</action>
<action name="addCategories">
<result>/add_categories.jsp</result>
</action>
<action name="addCategory" class="org.miraj.javabrains.actions.CategoryAction">
<!--<interceptor-ref name="defaultStack"/>-->
<!--<interceptor-ref name="fileUpload">-->
<!--<param name="allowedTypes">application/ms-excel,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,image/png,image/gif,image/jpeg</param>-->
<!--</interceptor-ref>-->
<result name="success">/add_category_success.jsp</result>
<result name="failure">/add_category_failure.jsp</result>
</action>
...
CategoryAction.java
public class CategoryAction extends ActionSupport implements ModelDriven<Category> {
Category category=new Category();
private String categoryError;
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
AdminServices adminServices=context.getBean("adminServices",AdminServices.class);
List<Category> categories;
//file upload handling
private File categoryImage;
private String categoryImageContentType;
private String categoryImageFileName;
private String destPath;
public String execute()
{
category.setReg_date(new Date());
if (adminServices.category_exists(category.getCategoryName())) {
setCategoryError("The Category already exists");
return "failure";
}
else
{
destPath="../uploads/";
try{
File destFile = new File(destPath, categoryImageFileName);
FileUtils.copyFile(categoryImage, destFile);
}catch(IOException e){
e.printStackTrace();
return "failure";
}
adminServices.addCategory(category);
return "success";
}
}
...
Category.java
package org.miraj.javabrains.models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@Entity
@Table (schema = "HIBERNATE")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String categoryName;
private String categoryDescription;
private String categoryImage;
private Date reg_date;
@OneToMany(mappedBy = "categoryId",cascade = CascadeType.PERSIST,fetch = FetchType.LAZY)
private Collection<Item> categoryItems=new ArrayList<Item>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String name) {
this.categoryName = name;
}
public String getCategoryDescription() {
return categoryDescription;
}
public void setCategoryDescription(String description) {
this.categoryDescription = description;
}
public Date getReg_date() {
return reg_date;
}
public void setReg_date(Date reg_date) {
this.reg_date = reg_date;
}
public void addCategoryItem(Item item)
{
categoryItems.add(item);
}
public String getCategoryImage() {
return categoryImage;
}
public void setCategoryImage(String categoryImage) {
this.categoryImage = categoryImage;
}
}
现在,当我上传图片时,我得到了值 categoryImageContentType, categoryImageFileName
但是,categoryImage为null并导致错误。
非常感谢任何帮助。 干杯
答案 0 :(得分:2)
1。将 categoryImage
的类型从 String
更改为 File
强>
2。我对该类型有疑问,您正用于保存类别图像。
检查数据库中“类别图像”列的类型。是 tinyblob
吗?如果是,则将其更改为 longblob
&amp;重试保存图像。
如果可能的话,也可以发布你的hibernate映射代码。