struts2文件上传到服务器的某个文件夹下



struts2文件上传到服务器的某个文件夹下。

  1. <img alt=”" src=”">

  1. 2、列表页
2、列表页
  1. <%@ page language=”java” contentType=”text/html; charset=UTF-8″%>
  2. <html>
  3. <head>
  4.     <title>Struts2 File Upload</title>
  5. </head>
  6. <body>
  7.     <form action=”<%=request.getContextPath()%>/group!upload.action” method=”POST” enctype=”multipart/form-data”>
  8.          选择文件:<input type=”file” name=”pic” size=”50″/><br/>
  9.        <input type=”submit” value=” 上传 “/>
  10.     </form>
  11. </body>
  12. </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%> 
<html> 
<head> 
    <title>Struts2 File Upload</title> 
</head> 
<body> 
    <form action="<%=request.getContextPath()%>/group!upload.action" method="POST" enctype="multipart/form-data">
         选择文件:<input type="file" name="pic" size="50"/><br/> 
       <input type="submit" value=" 上传 "/>       
    </form> 
</body> 
</html>
  1. 3.action
3.action
  1. <span style=”color: rgb(0, 0, 0);”></span><pre class=”java” name=”code”>import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.text.ParseException;
  5. import org.apache.commons.io.FileUtils;
  6. import org.apache.struts2.ServletActionContext;
  7. import com.opensymphony.xwork2.ActionContext;
  8. public class FileUploadAction {
  9.         //上传用
  10.         private File pic;
  11.         private String picFileName;
  12.         private String picContentType;
  13.         private InputStream inputStream;
  14.     /**
  15.      * 功能:文件上传
  16.      * @author 闫加盼
  17.      * @return 跳转页面的字符串
  18.      * @throws IOException
  19.      * @throws ParseException
  20.      */
  21.     public String upload(){
  22.         try {
  23.             //服务器的路径
  24.             String realpath = ServletActionContext.getServletContext().getRealPath(“/shuju”);
  25.             //例如——-D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
  26.             System.out.println(“上传到服务器的地址realpath: “+realpath);
  27.             //具体地址+文件名
  28.             String lj = realpath+”\\”+picFileName;
  29.             System.out.println(“具体的地址lujing”+lj);
  30.             //列如——  D:\apache-tomcat-7.0.47\webapps\dyda\shuju\15636139999[通话详单]查询_1_1.xls
  31.             if (pic != null) {
  32.                 File savefile = new File(new File(realpath), picFileName);
  33.                 if (!savefile.getParentFile().exists())
  34.                     savefile.getParentFile().mkdirs();
  35.                 FileUtils.copyFile(pic, savefile);
  36.                 ActionContext.getContext().put(“message”, “文件上传成功”);
  37.             }
  38.         } catch (IOException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         } catch (ParseException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.         return “success”;
  46.     }
  47.     public File getPic() {
  48.         return pic;
  49.     }
  50.     public void setPic(File pic) {
  51.         this.pic = pic;
  52.     }
  53.     public String getPicFileName() {
  54.         return picFileName;
  55.     }
  56.     public void setPicFileName(String picFileName) {
  57.         this.picFileName = picFileName;
  58.     }
  59.     public String getPicContentType() {
  60.         return picContentType;
  61.     }
  62.     public void setPicContentType(String picContentType) {
  63.         this.picContentType = picContentType;
  64.     }
  65.     public InputStream getInputStream() {
  66.         return inputStream;
  67.     }
  68.     public void setInputStream(InputStream inputStream) {
  69.         this.inputStream = inputStream;
  70.     }
  71. }
  72. </pre><br>
  73. <pre></pre>
  74. <pre></pre>
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.text.ParseException;
  5. import org.apache.commons.io.FileUtils;
  6. import org.apache.struts2.ServletActionContext;
  7. import com.opensymphony.xwork2.ActionContext;
  8. public class FileUploadAction {
  9. //上传用
  10. private File pic;
  11. private String picFileName;
  12. private String picContentType;
  13. private InputStream inputStream;
  14. /**
  15. * 功能:文件上传
  16. * @author 闫加盼
  17. * @return 跳转页面的字符串
  18. * @throws IOException
  19. * @throws ParseException
  20. */
  21. public String upload(){
  22. try {
  23. //服务器的路径
  24. String realpath = ServletActionContext.getServletContext().getRealPath(“/shuju”);
  25. //例如——-D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
  26. System.out.println(“上传到服务器的地址realpath: “+realpath);
  27. //具体地址+文件名
  28. String lj = realpath+”\\”+picFileName;
  29. System.out.println(“具体的地址lujing”+lj);
  30. //列如—— D:\apache-tomcat-7.0.47\webapps\dyda\shuju\15636139999[通话详单]查询_1_1.xls
  31. if (pic != null) {
  32. File savefile = new File(new File(realpath), picFileName);
  33. if (!savefile.getParentFile().exists())
  34. savefile.getParentFile().mkdirs();
  35. FileUtils.copyFile(pic, savefile);
  36. ActionContext.getContext().put(“message”, “文件上传成功”);
  37. }
  38. } catch (IOException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. } catch (ParseException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. return “success”;
  46. }
  47. public File getPic() {
  48. return pic;
  49. }
  50. public void setPic(File pic) {
  51. this.pic = pic;
  52. }
  53. public String getPicFileName() {
  54. return picFileName;
  55. }
  56. public void setPicFileName(String picFileName) {
  57. this.picFileName = picFileName;
  58. }
  59. public String getPicContentType() {
  60. return picContentType;
  61. }
  62. public void setPicContentType(String picContentType) {
  63. this.picContentType = picContentType;
  64. }
  65. public InputStream getInputStream() {
  66. return inputStream;
  67. }
  68. public void setInputStream(InputStream inputStream) {
  69. this.inputStream = inputStream;
  70. }
  71. }
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class FileUploadAction {

		//上传用
		private File pic;	
		private String picFileName;
		private String picContentType;
		private InputStream inputStream;

	/**
	 * 功能:文件上传
	 * @author 闫加盼
	 * @return 跳转页面的字符串
	 * @throws IOException
	 * @throws ParseException
	 */
	public String upload(){

		try {
			//服务器的路径
			String realpath = ServletActionContext.getServletContext().getRealPath("/shuju");        
			//例如-------D:\apache-tomcat-6.0.18\webapps\struts2_upload\images     
			System.out.println("上传到服务器的地址realpath: "+realpath); 
			//具体地址+文件名
			String lj = realpath+"\\"+picFileName;
			System.out.println("具体的地址lujing"+lj);
			//列如------	D:\apache-tomcat-7.0.47\webapps\dyda\shuju\15636139999[通话详单]查询_1_1.xls	
			if (pic != null) {            
				File savefile = new File(new File(realpath), picFileName);            
				if (!savefile.getParentFile().exists()) 
					savefile.getParentFile().mkdirs();  
				FileUtils.copyFile(pic, savefile);            
				ActionContext.getContext().put("message", "文件上传成功");        
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "success";
	}

	public File getPic() {
		return pic;
	}

	public void setPic(File pic) {
		this.pic = pic;
	}

	public String getPicFileName() {
		return picFileName;
	}

	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}

	public String getPicContentType() {
		return picContentType;
	}

	public void setPicContentType(String picContentType) {
		this.picContentType = picContentType;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

}
  1. 4.配置文件
  2. <?xml version=”1.0″ encoding=”UTF-8″?>
  3. <!DOCTYPE struts PUBLIC
  4.     “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
  5.     “http://struts.apache.org/dtds/struts-2.0.dtd”>
  6. <struts>
  7.     <!– 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
  8.         如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 –>
  9.     <constant name=”struts.action.extension” value=”action”/>
  10.     <!– 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭–>
  11.     <constant name=”struts.serve.static.browserCache” value=”false”/>
  12.     <!– 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开–>
  13.     <constant name=”struts.configuration.xml.reload” value=”true”/>
  14.     <!– 开发模式下使用,这样可以打印出更详细的错误信息–>
  15.     <constant name=”struts.devMode” value=”true”/>
  16.     <!– 默认的视图主题–>
  17.     <constant name=”struts.ui.theme” value=”simple”/>
  18.     <!–<constant name=”struts.objectFactory” value=”spring”/>–>
  19.     <!–解决乱码   –>
  20.     <constant name=”struts.i18n.encoding” value=”UTF-8″/>
  21.     <!– 指定允许上传的文件最大字节数。默认值是2097152(2M)–>
  22.     <constant name=”struts.multipart.maxSize” value=”10701096″/>
  23.     <package name=”upload” namespace=”/upload”extends=”struts-default”>
  24.         <action name=”group”class=”com.ljq.action.UploadAction” method=”upload()”>
  25.             <result name=”success”>/WEB-INF/page/message.jsp</result>
  26.         </action>
  27.     </package>
  28. </struts>


4.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
        如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <constant name="struts.action.extension" value="action"/>
    <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭-->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 开发模式下使用,这样可以打印出更详细的错误信息-->
    <constant name="struts.devMode" value="true"/>
    <!-- 默认的视图主题-->
    <constant name="struts.ui.theme" value="simple"/>
    <!--<constant name="struts.objectFactory" value="spring"/>-->
    <!--解决乱码   -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M)-->
    <constant name="struts.multipart.maxSize" value="10701096"/>

    <package name="upload" namespace="/upload"extends="struts-default">
        <action name="group"class="com.ljq.action.UploadAction" method="upload()">
            <result name="success">/WEB-INF/page/message.jsp</result>
        </action>
    </package>

</struts>
  1. 4.显示页面
  2. <%@ page language=”java”import=”java.util.*” pageEncoding=”UTF-8″%>
  3. <%@ taglib uri=”/struts-tags” prefix=”s”%>
  4. <!DOCTYPE HTML PUBLIC”-//W3C//DTD HTML 4.01 Transitional//EN”>
  5. <html>
  6.   <head>
  7.     <title>上传成功</title>
  8.     <meta http-equiv=”pragma” content=”no-cache”>
  9.     <meta http-equiv=”cache-control” content=”no-cache”>
  10.     <meta http-equiv=”expires” content=”0″>
  11.   </head>
  12.   <body>
  13.     上传成功!
  14.     <br/><br/>
  15.     <!– ${pageContext.request.contextPath} tomcat部署路径,
  16.           如:D:\apache-tomcat-6.0.18\webapps\struts2_upload\–>
  17.     <img src=”${pageContext.request.contextPath}/<s:property value=”‘images/’+imageFileName”/>”>
  18.     <s:debug></s:debug>
  19.   </body>
  20. </html>
4.显示页面

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>上传成功</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
  </head>

  <body>
    上传成功!
    <br/><br/>
    <!-- ${pageContext.request.contextPath} tomcat部署路径,
          如:D:\apache-tomcat-6.0.18\webapps\struts2_upload\-->
    <img src="${pageContext.request.contextPath}/<s:property value="'images/'+imageFileName"/>">
    <s:debug></s:debug>
  </body>
</html>

 

前台file的name值必须和action里的值一样 

 

http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html+keep_trying

http://blog.csdn.net/yjp198713/article/details/16887361