<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 89, comments - 241, trackbacks - 0, articles - 1
       :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

    Struts2文件上傳以及空指針異常解決

    Posted on 2009-09-27 08:54 saobaolu 閱讀(4339) 評論(2)  編輯  收藏 所屬分類: java基礎與算法

    uploadFile.java

     1package action;
     2
     3import java.io.File;    
     4import java.text.DateFormat;    
     5import java.text.SimpleDateFormat;    
     6import java.util.Date;    
     7import java.util.Random;    
     8   
     9import javax.servlet.ServletContext;    
    10   
    11import org.apache.commons.io.FileUtils;    
    12import org.apache.struts2.util.ServletContextAware;    
    13   
    14import com.opensymphony.xwork2.ActionSupport;    
    15   
    16public class uploadFile extends ActionSupport implements ServletContextAware {    
    17        
    18    private static final long serialVersionUID = -5016873153441103539L;    
    19        
    20    private File doc;    
    21    private String fileName;    
    22    private String contentType;    
    23        
    24    private ServletContext context;    
    25            
    26    public void setDoc(File file) {    
    27        this.doc = file;    
    28    }
        
    29        
    30    public void setDocFileName(String fileName) {    
    31        this.fileName = fileName;    
    32    }
        
    33        
    34    public void setDocContentType(String contentType) {    
    35        this.contentType = contentType;    
    36    }
        
    37        
    38    public void setServletContext(ServletContext context) {    
    39        this.context = context;    
    40    }
        
    41        
    42    public String execute() throws Exception {    
    43        String targetDirectory = context.getRealPath("/upload");    
    44        String targetFileName = generateFileName(fileName);    
    45        File target = new File(targetDirectory, targetFileName);    
    46            
    47        FileUtils.copyFile(doc, target);    
    48            
    49        return SUCCESS;    
    50    }
        
    51        
    52    private String generateFileName(String fileName) {    
    53        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");    
    54        String formatDate = format.format(new Date());    
    55            
    56        int random = new Random().nextInt(10000);    
    57            
    58        int position = fileName.lastIndexOf(".");    
    59        String extension = fileName.substring(position);    
    60            
    61        return formatDate + random + extension;    
    62    }
           
    63}
       
    64

    struts.xml
     1<?xml version="1.0" encoding="UTF-8" ?>   
     2<!DOCTYPE struts PUBLIC    
     3        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
     4        "http://struts.apache.org/dtds/struts-2.0.dtd">   
     5<struts>      
     6    <package name="uploadFile" extends="struts-default">    
     7        <action name="uploadFile" class="action.uploadFile">   
     8            <result>/jsp/up.jsp</result>   
     9        </action>           
    10    </package>      
    11</struts>   
    web.xml(Struts2的xml,非上傳的xml)
     1<?xml version="1.0" encoding="UTF-8"?>   
     2<web-app version="2.4"     
     3    xmlns="http://java.sun.com/xml/ns/j2ee"     
     4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
     5    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     
     6    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
     7    <filter>   
     8        <filter-name>struts2</filter-name>   
     9        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
    10    </filter>   
    11    <filter-mapping>   
    12        <filter-name>struts2</filter-name>   
    13        <url-pattern>/*</url-pattern>   
    14    </filter-mapping>      
    15</web-app>   

    up.jsp(上傳成功頁面)
     1<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2<%@ taglib prefix="s" uri = "/struts-tags" %>
     3<%
     4String path = request.getContextPath();
     5String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6
    %>
     7
     8<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9<html>
    10  <head>
    11    <base href="<%=basePath%>">
    12    
    13    <title>My JSP 'up.jsp' starting page</title>
    14    
    15    <meta http-equiv="pragma" content="no-cache">
    16    <meta http-equiv="cache-control" content="no-cache">
    17    <meta http-equiv="expires" content="0">    
    18    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    19    <meta http-equiv="description" content="This is my page">
    20    <!--
    21    <link rel="stylesheet" type="text/css" href="styles.css">
    22    -->
    23
    24  </head>
    25  
    26  <body>
    27    <s:property value="contentType"/><br />
    28    <s:property value="dir" /><br />
    29  </body>
    30</html>
    31
    上傳頁面:
     1<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2<%@ taglib prefix="s" uri="/struts-tags" %>
     3<%
     4String path = request.getContextPath();
     5String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6
    %>
     7
     8<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9<html>
    10  <head>
    11    <base href="<%=basePath%>">
    12    
    13    <title>最新產品發布頁</title>
    14    
    15    <meta http-equiv="pragma" content="no-cache">
    16    <meta http-equiv="cache-control" content="no-cache">
    17    <meta http-equiv="expires" content="0">    
    18    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    19    <meta http-equiv="description" content="This is my page">
    20    <!--
    21    <link rel="stylesheet" type="text/css" href="styles.css">
    22    -->
    23
    24  </head>
    25  
    26  <body>
    27    <s:form action="uploadFile" method="post" enctype="multipart/form-data">
    28        <s:file name = "doc" label="上傳視頻" />
    29        <s:submit value="上傳" />
    30    </s:form>
    31  </body>
    32</html>
    33

    很容易就會報錯,空指針異常。
    原因在于上傳頁面<s:file name="這里面的值與action的值不匹配" />
    可是我上傳成功之后,up.jsp頁面沒有傳入任何值,郁悶了~
    新手,多指點指點,謝謝~

    沒有所謂的命運,只有不同的選擇!
    主站蜘蛛池模板: 亚洲av乱码中文一区二区三区| 亚洲熟妇av一区二区三区漫画| 亚洲国产美女在线观看| 免费人成毛片动漫在线播放 | baoyu116.永久免费视频| 国产亚洲精品拍拍拍拍拍| 亚洲五月午夜免费在线视频| 亚洲精品视频免费观看| 国产日韩久久免费影院| 亚洲av无码不卡一区二区三区| 99久久免费国产精品热| 亚洲成色WWW久久网站| 8x8x华人永久免费视频| 亚洲w码欧洲s码免费| 免费av欧美国产在钱| 相泽南亚洲一区二区在线播放| 四虎永久在线精品免费影视 | 无码国产精品一区二区免费| 最新亚洲精品国偷自产在线| 日本黄页网站免费| 特a级免费高清黄色片| 亚洲国产精品无码久久一区二区| 久久青草91免费观看| 自拍日韩亚洲一区在线| 成人免费视频国产| 岛国精品一区免费视频在线观看 | 精品无码国产污污污免费| 国产精品亚洲小说专区| 中文字幕精品亚洲无线码一区应用| 日本免费A级毛一片| 91亚洲一区二区在线观看不卡| 黄瓜视频高清在线看免费下载| 国内成人精品亚洲日本语音| 亚洲精品白浆高清久久久久久| 日韩中文字幕精品免费一区| 青青免费在线视频| 久久夜色精品国产噜噜噜亚洲AV| 免费看韩国黄a片在线观看| 一级做a爰片久久免费| 亚洲欧洲日本天天堂在线观看| 免费看国产一级片|