分享

struts2学习笔记(十) 利用fileUpload实现文件的上传

 levinLee 2010-07-24
    今天用struts2 的fileupload 实现了文件的上传。。
   1 web.xml
  <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java./xml/ns/javaee"
 xmlns:xsi="http://www./2001/XMLSchema-instance"
 xsi:schemaLocation="http://java./xml/ns/javaee
 http://java./xml/ns/javaee/web-app_2_5.xsd">
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
 
</web-app>
2 struts.xml
        <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts./dtds/struts-2.0.dtd" >
<struts>
   <include file="struts-default.xml"></include>
   <!-- 这里设置编码方式,注意上传页面也要用此编码方式,否则得到的文件名是乱码 -->
   <constant name="struts.i18n.encoding" value="gbk"></constant>
   <constant name="struts.multipart.saveDir" value="d:\struts2upload\"></constant>
 
   <package name="struts2ofupload" extends="struts-default">
       <action name="upload" class="com.struts2.action.UploadAction">
          <result name="show">/show.jsp</result>
              <result>/index.jsp</result>
          <result name="input">/index.jsp</result>
     
<!--  定义拦截器 拦截上传的文件格式,上传文件的大小 注:文件格式可以参考tomcat下conf下web.xml 的一些文件格式 -->     
     <interceptor-ref name="fileUpload">
          <param name="allowedTypes">application/vnd.ms-powerpoint,image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/jpg,application/msword,audio/x-mpeg,text/html,text/plain</param>
          <param name="maximumSize">8192000</param>
          </interceptor-ref>
          <interceptor-ref name="defaultStack"></interceptor-ref>
       </action>
   </package>
</struts>
 
3 action
     package com.struts2.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
 private List<File> file;
 private List<String> fileFileName;
 private List<String> fileContentType;
 public List<File> getFile() {
 return file;
 }
 public void setFile(List<File> file) {
 this.file = file;
 }
 public List<String> getFileContentType() {
 return fileContentType;
 }
 public void setFileContentType(List<String> fileContentType) {
 this.fileContentType = fileContentType;
 }
 public List<String> getFileFileName() {
 return fileFileName;
 }
 public void setFileFileName(List<String> fileFileName) {
 this.fileFileName = fileFileName;
 }
 /**
 * 动态上传文件
 * @return
 * @throws Exception
 */
 public String Upload() throws Exception {
 InputStream is=null;
 OutputStream ops=null;
     for(int i=0;i<file.size();i++){
     try{
 is=new FileInputStream(file.get(i));
 String root=ServletActionContext.getRequest().getRealPath("/uploads");
 File destFile=new File(root+"/",this.getFileFileName().get(i));
 //File destFile=new File(root,this.getFileFileName().get(i));
 ops=new FileOutputStream(destFile);
 byte [] b=new byte[400];
 int length=0;
 while((length=is.read(b))>0){
 ops.write(b,0,length);
 //ops.write(b); 这样子同样可行
 }
 }catch(Exception ex){
 ex.printStackTrace();
 }finally{
 is.close();
 ops.close();
 }
     }
 return "show";
 }
}
4 jsp
  <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'upload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function addMore(){
var td=document.getElementById("more");
var br=document.createElement("br");
var input=document.createElement("input");
var button=document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="-";
button.onclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
  </head>
 
  <body>
   <s:form action="upload!Upload.action" method="post" enctype="multipart/form-data">
   <table align="center" width="60%" border="1">
   <tr>
   <td>
   选择上传的文件:
   </td>
   <td id="more">
   <%--<s:file name="file" label="选择上传的文件"></s:file>--%>
   <input type="file" name="file">
   <input type="button" value="+" onclick="addMore()"/>
   </td>
   </tr>
   <tr>
   <td>
   </td>
   <td>
   <s:submit value="上传" align="center"></s:submit>
   </td>
   </tr>  
   </table>
   </s:form>
  </body>
</html>
 
   我试了下,上传成功,比较好用。
  可以通过设置<param name="allowedTypes">来设定上传文件的类型,<param name="maximumSize">8192000</param>来设置上传文件的最大极限。


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多