zoukankan      html  css  js  c++  java
  • Struts2的简单的文件上传

    1文件上传的流程:

      第一步:首先得用表单标签的<s:file>在客户端接收上传的文件例如:

     1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
     2 <%@ taglib prefix="s" uri="/struts-tags" %>
     3 <html>
     4   <head>
     5   <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
     6   </head>
     7   <body>
     8     <s:form action="testFileUpload" method="post" enctype ="multipart/form-data">
     9         <s:file name="wordFile" label="WordFile"></s:file>------<s:file>代表接收文件在上传客户端的具体位置
    10         <s:textfield name="wordDesc" label="PPTDESC"></s:textfield>
    11         <s:submit></s:submit>
    12     </s:form>
    13   </body>
    14 </html>

        第二步:在Action中声明三个属性,并声明get和Set方法

        1FileFieldName(名字要与form表单的<s:filr>标签的name属性相同):接收form表单中的wordFile的值,也就是接收文件在客户端的实际路径

        例如:private File wordFile;

               2FileFieldNameContentType:上传的文件属性;示例:private String wordFileContentType

              3 FileFieldNameFileName:这个代表上传的文件名,示例:private Sting wordFileFileName,例如上传的文件是index.jsp,那wordFileFileName=index.jsp

        因为2和3都是上传文件的属性,所以都会带有上传时<s:file>标签时定义的方法

       代码示例:

     1 package com.atguigu.struts2.FileUpload.app;
     2 
     3 import java.io.File;
     4 
     5 import com.opensymphony.xwork2.ActionSupport;
     6 
     7 public class TestAction extends ActionSupport {
     8 
     9     /**
    10      * 
    11      */
    12     private static final long serialVersionUID = 1L;
    13     private File wordFile;
    14     private String wordFileContentTypeString;
    15     private String wordFileFileName;
    16     /**
    17      * @return the wordFile
    18      */
    19     public File getWordFile() {
    20         return wordFile;
    21     }
    22     /**
    23      * @param wordFile the wordFile to set
    24      */
    25     public void setWordFile(File wordFile) {
    26         this.wordFile = wordFile;
    27     }
    28     /**
    29      * @return the wordFileContentTypeString
    30      */
    31     public String getWordFileContentTypeString() {
    32         return wordFileContentTypeString;
    33     }
    34     /**
    35      * @param wordFileContentTypeString the wordFileContentTypeString to set
    36      */
    37     public void setWordFileContentTypeString(String wordFileContentTypeString) {
    38         this.wordFileContentTypeString = wordFileContentTypeString;
    39     }
    40     /**
    41      * @return the wordFileFileName
    42      */
    43     public String getWordFileFileName() {
    44         return wordFileFileName;
    45     }
    46     /**
    47      * @param wordFileFileName the wordFileFileName to set
    48      */
    49     public void setWordFileFileName(String wordFileFileName) {
    50         this.wordFileFileName = wordFileFileName;
    51     }
    52     public String execute() throws Exception {
    53         // TODO Auto-generated method stub
    54         return super.execute();
    55     }
    56 }

    文件上传?何为文件上传,就是把客户端的文件读取了然后写入服务里面吧,所以必须要使用到输入和输出流

    第三步:声明输入流和输出流 ,把文件写到自己指定的服务器位置:

      1)先设置文件的存储位置吧:servletContext.getRealPath("/files/" + wordFileFileName);设置文件放在根目录的files文件夹里面,

                     wordFileFileName:代表放入files文件夹时上传文件的名

         2)读取上传的文件吧,并把文件写入我们指定的位置

    FileInputStream in = new FileInputStream(ppt);/把文件放入输入流中
    
    FileOutputStream out = new FileOutputStream(dir);
    
    byte [] buffer = new byte[1024];
    int len = 0;
    
    while((len = in.read(buffer)) != -1){//从输入流读取上传文件的消息
    out.write(buffer, 0, len);//把上传文件写入我们的服务器的files文件夹的wordFileFileName文件中
    }

     完整示例代码:
    1
    ServletContext servletContext=ServletActionContext.getServletContext(); 2 String dir=servletContext.getRealPath("/File/"+pptFileName); 3 FileInputStream fis=new FileInputStream(ppt); 4 FileOutputStream fos=new FileOutputStream(dir); 5 byte [] buffer = new byte[1024];//因为FileinputStream是字节流,所以要声明一个字节数组来存放从文件读出来的字节 6 int len = 0; 7 while((len = fis.read(buffer)) != -1){ //fis.read()方法读取文件,没有读取完就返回0读取完就返回-1 8 fos.write(buffer, 0, len); 9 } 10 fos.close(); 11 fis.close();
  • 相关阅读:
    php小结
    HTML-WEB前端-photoshop切图抠图详解
    JS面向对象的程序设计
    AJAX同步与异步的区别
    数据库的优化
    phpcms网站搬家 至 服务器 完整并且详细过程
    phpcms网页替换验证码功能 及 搜索功能
    用phpcms切换中英文网页的方法(不用解析二级域名)、phpcms完成pc和手机端切换(同一域名)
    JS常用屏蔽代码
    信息安全政策(隔离与监控)
  • 原文地址:https://www.cnblogs.com/jeremy-blog/p/3995284.html
Copyright © 2011-2022 走看看