当前位置: 代码迷 >> Java Web开发 >> 生手学习struts2提供的上传但是娶不到上传的文件
  详细解决方案

生手学习struts2提供的上传但是娶不到上传的文件

热度:173   发布时间:2016-04-16 21:44:45.0
新手学习struts2提供的上传但是娶不到上传的文件
一个表单
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=[code=java]utf
-8">
<title>来上传</title>
</head>
<body>
   <form action="doUpload" enctype="multipart/form-data">
      <input type="file" name="upload" value="">
      <input type="submit" name="submit" value="上传图片">
   </form>
   
</body>
</html>[/code]
package action;

   import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

   public class uploadaction extends ActionSupport {
      
  private File uploadfile;
      private String uploadcontentType;
      private String uploadfilename;

      

    
public File getUploadfile() {
return uploadfile;
}




public void setUploadfile(File uploadfile) {
this.uploadfile = uploadfile;
}




public String getUploadcontentType() {
return uploadcontentType;
}




public void setUploadcontentType(String uploadcontentType) {
this.uploadcontentType = uploadcontentType;
}




public String getUploadfilename() {
return uploadfilename;
}




public void setUploadfilename(String uploadfilename) {
this.uploadfilename = uploadfilename;
}




public String doUpload() {
        System.out.println(uploadfilename);
return SUCCESS;
      }

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>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />

    <package name="upload" namespace="/" extends="struts-default">
    <action name="doUpload" class="action.uploadaction" method="doUpload">
         <result>uploadsuccess.jsp</result>
    </action> 
         
</package>
</struts>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>upload</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!--  添加struts2的过滤器  
  -->  
   <filter>  
  <filter-name>struts2</filter-name>  
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>  
  <filter-mapping>  
  <filter-name>struts2</filter-name>  
  <url-pattern>/*</url-pattern>  
  </filter-mapping>  
  
  
</web-app>

按照视频大的   我确定视频中没有拦截器的
------解决思路----------------------
action中
private File uploadfile;
要和前台这个name一直
<input type="file" name="upload" value="">
可以参考
http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html


------解决思路----------------------
http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990688.html
  相关解决方案