posts - 68, comments - 19, trackbacks - 0, articles - 1

Struts2的上传

Posted on 2012-02-28 11:24 viery 阅读(245) 评论(0)  编辑  收藏 所属分类: Struts2
Struts2自带上传下载的实现:
1.编写输入页面和结果页面
input.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
<base href="<%=basePath%>">
    
    
<title>My JSP 'index.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 language="JavaScript">
    
function addComponent()
    
{
        
var td=document.getElementById("files");
        
var br=document.createElement("<br>");
        
var input=document.createElement("input");
        
var button=document.createElement("input");

        input.name
="uploads";
        input.type
="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>
     
<input type="button" onclick="addComponent();" value="添加文件" />
        
<br />
        
<form onsubmit="return true;" action="upload"
            method
="post" enctype="multipart/form-data">
            
<table>
            
<tr>
            
<td id="files">
          
<input type='file' name='uploads' />
            
</td>
           
</tr>
           
</table>
       
            
<input type="submit" value="上传" />
        
</form>
      
<s:fielderror/>
  
</body>
</html>
js用于自动生成和控制删除上传控件的数量。
result.jsp
 
  <body>
    This a struts page. <br>
  </body>

2.配置struts.xml


<?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>
    
<constant name="struts.custom.i18n.resources" value="message"></constant>
    
<constant name="struts.i18n.encoding" value="gbk"/>
    
<constant name="struts.multipart.saveDir" value="f:\"/>
    
<constant name="struts.multipart.maxSize" value="2097152000"/>
    
<package name="JsUpload" extends="struts-default">
        
<action name="upload" class="org.vle.action.UploadAction">
            
<result>/result.jsp</result>
            
<result name="input">/upload.jsp</result>
            
<interceptor-ref name="fileUpload">
                
<param name="maximumSize">10240000</param>
                
<param name="allowedTypes">application/vnd.ms-powerpoint</param>
            
</interceptor-ref>
            
<interceptor-ref name="defaultStack"></interceptor-ref>
        
</action>
    
</package>

</struts>

3实现action



package org.vle.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
    
    
private List<File> uploads;
    
private List<String> uploadsFileName;
    
private List<String>  uploadsContentType;

    

    
public List<String> getUploadsContentType() {
        
return uploadsContentType;
    }


    
public void setUploadsContentType(List<String> uploadsContentType) {
        
this.uploadsContentType = uploadsContentType;
    }


    
public List<File> getUploads() {
        
return uploads;
    }


    
public void setUploads(List<File> uploads) {
        
this.uploads = uploads;
    }


    



    
public List<String> getUploadsFileName() {
        
return uploadsFileName;
    }


    
public void setUploadsFileName(List<String> uploadsFileName) {
        
this.uploadsFileName = uploadsFileName;
    }




    @Override
    
public String execute() throws Exception {
        
            
for(int i=0;i<uploads.size();i++){
                InputStream is
=new FileInputStream(uploads.get(i));
                String name
=uploadsFileName.get(i);
                String type
=uploadsContentType.get(i);
                System.out.println(type);
                String path
=ServletActionContext.getRequest().getRealPath("/temp");
                File f
=new File(path,name);
                File temp
=new File(path);
                
if(!temp.exists()){
                    temp.mkdirs();
                }

                OutputStream os
=new FileOutputStream(f);
                
byte[] b=new byte[1024];
                
int len;
                
while((len=is.read(b))>0){
                    os.write(b, 
0, len);
                }

                os.close();
                is.close();
            }

            
    
        
        
        
return Action.SUCCESS;
    }

}




只有注册用户登录后才能发表评论。


网站导航: