| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
9年前发布

Struts2文件的上传实例

需要的包文件:


commons-fileupload-1.2.1.jar  commons-io-1.3.2.jar  commons-logging-1.1.jar  freemarker-2.3.13.jar  ognl-2.6.11.jar  struts2-core-2.1.6.jar  xwork-2.1.2.jar

Struts2Test.java源代码:



package com.test;    import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;    import org.apache.struts2.ServletActionContext;  import com.opensymphony.xwork2.ActionSupport;    @SuppressWarnings("serial")  public class Struts2Test extends ActionSupport{      private String picFileName;   private File pic;      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 upload() throws IOException {    //输出的文件路径以及文件名java.io.File.File(String parent, String child)    File upPic=new File(ServletActionContext.getServletContext().getRealPath("upload"),picFileName);    FileInputStream in=null;    FileOutputStream out=null;    //得到父类路径,如果不存在则创建    upPic.getParentFile().mkdirs();    in=new FileInputStream(pic);   //读入文件    out=new FileOutputStream(upPic);   //输出文件    int len=0;    //数据长度    byte[] byt=new byte[1024];   //每次读入的数据包大小    while((len=in.read(byt))!=-1){     //如果有数据读入则输出     out.write(byt, 0, len);    }    in.close();   //关闭读入流    out.close();    //关闭输出流    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.devMode" value="true" />  <package name="default" extends="struts-default" namespace="/">   <action name="hello" class="com.test.Struts2Test" >    <result name="success">/success.jsp</result>   </action>  </package>   </struts>    

web.xml源代码:



<?xml version="1.0" encoding="UTF-8"?>  <web-app id="WebApp_9" 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">    <welcome-file-list>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list>        <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>

index.jsp源代码:



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  <%  String path = request.getContextPath();  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  %>  <%@taglib prefix="s" uri="/struts-tags"%>  <html>    <head>      <base href="<%=basePath%>">      <title>My JSP 'index.jsp' starting page</title>    </head>    <body>     <s:form action="hello!upload" enctype="multipart/form-data" method="post">     <!-- enctype="multipart/form-data"   此处是一个很容易忽略的盲点 -->      <s:file name="pic" label="上传" />      <s:submit value="提交"/>     </s:form>    </body>  </html>

success.jsp源代码:



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  <%  String path = request.getContextPath();  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  %>  <html>    <head>      <base href="<%=basePath%>">      <title>SUCCESS</title>    </head>    <body>      SUCCESS! <br>    </body>  </html>
来自:http://blog.csdn.net/zzy1078689276/article/details/47406209


 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1439296645942.html
Struts2 Web框架