2016-01-02 21 views
-1

我想在使用struts 2時實現文件上傳功能。但是我得到錯誤「Source must not null」。當我調試源代碼時(上傳)文件數據顯示爲空。在Struts2中的文件上傳問題 - 源文件不能爲空

以下是我的代碼,

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爲空並導致錯誤。

任何幫助,非常感謝。 乾杯

+0

字符串categoryImage是可疑:) –

+1

感謝@AndreaLigios,你保存了一天;) –

回答

2

變化categoryImageStringFile

2.我對類型無疑是這個類型,你正在使用保存類別圖像。

檢查數據庫中的類別圖像列的類型。是tinyblob?如果是,則將其更改爲longblob &重試以保存圖像。 如果可能的話,發佈你的hibernate映射代碼。

+0

是的,你是對的,問題是我在模型和jsp中使用了相同的名稱categoryImage。我不想將圖像保存在數據庫中,而只保存名稱(稍後我將用它來引用圖像),所以我在模型中使用了一個String變量。 但是由於在jsp和Action類中它的類型是File,並且由於這是模型驅動的結構,因此會發生此問題。我改變了模型中的變量名稱並將categoryImageFileName賦值給它。然後我得到了動作類中categoryImage的真實文件值。 –