java代码:package com;  
import java.io.File;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.Random;  
import org.apache.commons.io.FileUtils;  
import org.apache.struts2.ServletActionContext;  
import com.opensymphony.xwork2.ActionSupport;  
 
public class FileUploadAction extends ActionSupport  
{  
    private static final long serialVersionUID = 4658947796066228597L;  
    private File doc;  
    private String fileName;  
    private String contentType;  
 
    public void setDoc(File file)  
    {  
        this.doc = file;  
    }  
 
    public void setDocFileName(String fileName)  
    {  
        this.fileName = fileName;  
    }  
 
    public void setDocContentType(String contentType)  
    {  
        this.contentType = contentType;  
    }  
 
    @Override  
    public String execute() throws Exception  
    {  
        System.out.println("fileName=" + fileName);  
        System.out.println("contextType=" + contentType);  
        // 得到当前web工程下的/files目录的在本机的绝对路径,如果没有这个文件夹则会创建  
        String targetDirectory = ServletActionContext.getServletContext()  
                .getRealPath("/files");  
        //重命名上传文件  
        String targetFileName = generateFileName(fileName);  
        //在指定目录创建文件  
        File target = new File(targetDirectory, targetFileName);  
        //把要上传的文件copy过去  
        FileUtils.copyFile(doc, target);  
        return SUCCESS;  
    }  
    //重命名上传文件(非必须)  
    public String generateFileName(String fileName)  
    {  
        String formatDate = new SimpleDateFormat("yyMMddHHmmss")  
                .format(new Date());  
        int random = new Random().nextInt(10000);  
        int position = fileName.lastIndexOf(".");  
        String extension = fileName.substring(position);  
 
        return formatDate + random + extension;  
    }  
 
    public String getFileName()  
    {  
        return fileName;  
    }  

jsp 代码:
<%@ page language="java" pageEncoding="GB18030"%> 
<%@taglib prefix="s" uri="/struts-tags" %> 
<html> 
  <head> 
    <title>Upload Page</title> 
  </head> 
  <body> 
  <s:form action="upload" method="post" enctype="multipart/form-data"> 
      <s:file name="doc" label="File"/> 
      <s:submit value="upload"/> 
  </s:form> 
  </body> 
</html> 
web.xml 代码<?xml version="1.0" encoding="UTF-8"?>     
<web-app version="2.4"       
    xmlns="http://java.sun.com/xml/ns/j2ee"       
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee       
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">     
          
    <filter>     
        <filter-name>struts-cleanup</filter-name>     
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>     
    </filter>     
          
    <filter>     
        <filter-name>struts2</filter-name>     
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>     
    </filter>     
          
    <filter-mapping>     
        <filter-name>struts-cleanup</filter-name>     
        <url-pattern>/*</url-pattern>     
    </filter-mapping>     
          
    <filter-mapping>     
        <filter-name>struts2</filter-name>     
        <url-pattern>/*</url-pattern>     
    </filter-mapping>     
 
</web-app>    
......
debug 运行结果: fileName = null
contextType : = null

解决方案 »

  1.   

    File doc的setter方法,貌似参数必须是doc才行把。。而且fileName必须是docFileName。。
      

  2.   


    private String docFileName;   
    private String docContentType;
      

  3.   

    fileName肯定为空了,名字都没写正确。参考2楼的
      

  4.   

    private File doc;   
    private String fileName;   
    private String contentType;这里的命名规范是非常严格的,必须这样写:
    private File doc;   
    private String docFileName;   
    private String docContentType;