CSDN博客

img epng54

文件上传 下载

发表于2008/10/3 18:49:00  427人阅读

<%@ page language="java" pageEncoding="gbk"%> 
 
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> 
 
<html:html lang="true"> 
  <head> 
    <title>addfile.jsp</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"> 
      <SCRIPT LANGUAGE="JavaScript">    
    <!--     
    var n = 1;     
        function add(){     
                if(n>5){  
                    alert("不能在加了");  
                    return;  
                }  
                var otable = document.getElementById("tablee");     
                var orow=otable.insertRow();      
                var ocol2 = orow.insertCell();      
                var ocol3 = orow.insertCell();      
                var ocol4 = orow.insertCell();      
                ocol2.innerHTML ="<input type=file name=files+"+n+">";     
                ocol3.innerHTML ="<input type='button' value='添加' onClick='add()' />";     
                ocol4.innerHTML ="<input type='button' value='删除' onClick='javascript:deleteRow();return false;' />";     
                n++;     
                     
        }     
    
        function deleteRow(){     
            if(n<=1){  
                alert("不能在删了");  
                return;  
            }  
            var otable = document.getElementById("tablee");     
        //  var rowIndex = event.srcElement.parentElement.parentElement.parentElement.rowIndex;     
        var rowIndex =event.srcElement.parentElement.parentElement.rowIndex     
        /*     
        event 事件     
        srcElement  button     
        parentElement 上1层td     
        parentElement 上1层tr     
        rowIndex tr的rowIndex      
        */     
            otable.deleteRow(rowIndex);    
            n--;   
        }     
    //-->    
    </SCRIPT>       
  </head> 
    
  <body> 
    <html:form action="/file.do?method=add" enctype="multipart/form-data"> 
            <table id="tablee">    
        <tr>    
            <td>文件上传</td>    
            <td> 添加</td>    
            <td> 删除</td>    
        </tr>    
         <tr>    
            <td> <input type="file" name="files"> </td>    
            <td> 
            <input type="button" value="添加" onClick="add()" /> 
            </td>   
            <td> 
            <input type="button" value="删除" onClick="deleteRow()" />   
            </td>   
        </tr>    
          
    </table>    
          <html:submit></html:submit> 
    </html:form> 
  </body> 
</html:html> 

 

 

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ page import="com.epng.use.dbupload"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<HTML>
 <HEAD>
  <TITLE>上传成功!</TITLE>
  <META content="text/html; charset=gb2312" http-equiv=Content-Type>
 </HEAD>
 <BODY leftMargin=0 topMargin=0>
  
  <table width="80%" border="0" cellpadding="0" cellspacing="0"
   bgcolor="#DEE7EF">
   <tr>
    <td align="center">
     <%
      String fileName = null;
      mySmartUpload.initialize(pageContext);
      mySmartUpload.upload();
      
      for(int i=0;i<mySmartUpload.getFiles().getCount();i++)
      {
       com.jspsmart.upload.SmartFile myFile = mySmartUpload.getFiles().getFile(i);
      if (!myFile.isMissing()) {
       String ext = myFile.getFileExt();//得到后缀  
       fileName = myFile.getFileName();
       myFile.saveAs("/files/" + fileName);//你要存放文件所在文件夹的相对路径
       dbupload db = new dbupload();
       db.insert(fileName, ext);
       out.println("文件:<b>" + fileName + "</b>上传成功!<br>文件大小:"
       + myFile.getSize() + "b<BR>");
      }
      }
     %>
    
 </BODY>
</HTML>

 

----------------------------------struts-----------------------

 

 

<%@ page language="java" pageEncoding="ISO-8859-1"%>  
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>   
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>  
   
<html>   
    <head>  
        <title>JSP for FileForm form</title>  
    </head>  
    <body>  
        <html:form action="/file" enctype="multipart/form-data">  
        <html:file property="file1"></html:file>  
            <html:submit/><html:cancel/>  
        </html:form>  
    </body>  
</html> 

 

 

 

package com.epng.struts.action;

import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.epng.struts.form.FileForm;
 
 
 
public class FileAction extends Action {  
    /* 
     * Generated Methods 
     */ 
 
    /**  
     * Method execute 
     * @param mapping 
     * @param form 
     * @param request 
     * @param response 
     * @return ActionForward 
     */ 
    public ActionForward execute(ActionMapping mapping, ActionForm form,  
            HttpServletRequest request, HttpServletResponse response) {  
        FileForm fileForm = (FileForm) form;  
        FormFile file1=fileForm.getFile1();  
        if(file1!=null){  
            //上传路径  
            String dir=request.getSession(true).getServletContext().getRealPath("/upload");  
            OutputStream fos=null;  
            try {  
                fos=new FileOutputStream(dir+"/"+file1.getFileName());  
                fos.write(file1.getFileData(),0,file1.getFileSize());  
                fos.flush();  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }finally{  
                try{  
                fos.close();  
                }catch(Exception e){}  
            }  
        }  
        //页面跳转  
        return mapping.findForward("success");  
    }  

 

 

 

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.epng.struts.form;
 

import javax.servlet.http.HttpServletRequest;  
import org.apache.struts.action.ActionErrors;  
import org.apache.struts.action.ActionForm;  
import org.apache.struts.action.ActionMapping;  
import org.apache.struts.upload.FormFile;  
 
 
public class FileForm extends ActionForm {  
    /* 
     * Generated Methods 
     */ 
    private FormFile file1;  
    /**  
     * Method validate 
     * @param mapping 
     * @param request 
     * @return ActionErrors 
     */ 
    public ActionErrors validate(ActionMapping mapping,  
            HttpServletRequest request) {  
        // TODO Auto-generated method stub  
        return null;  
    }  
 
    /**  
     * Method reset 
     * @param mapping 
     * @param request 
     */ 
    public void reset(ActionMapping mapping, HttpServletRequest request) {  
        // TODO Auto-generated method stub  
    }  
 
    public FormFile getFile1() {  
        return file1;  
    }  
 
    public void setFile1(FormFile file1) {  
        this.file1 = file1;  
    }  

0 0

相关博文

我的热门文章

img
取 消
img