zoukankan      html  css  js  c++  java
  • struts2-17-上传单个文件

    一:上传文件的主页面  upload.jsp

         注意: <input type="file" name="upload">

                <form  enctype="multipart/form-data">//二进制转换

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib prefix="s" uri="/struts-tags" %>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>上传文件主页面</title>
     9 </head>
    10 <body>
    11 <s:fielderror/>
    12   <form action="uploadAction" method="post" enctype="multipart/form-data">
    13     <table border="1">
    14      <caption>上传文件</caption>
    15      <tr>
    16        <th>上传标题</th>
    17        <td><input type="text" name="title"></td>
    18      </tr>
    19      <tr>
    20        <th>选择文件</th>
    21        <td><input type="file" name="upload"></td>
    22      </tr>
    23      <tr>
    24        <td colspan="2" align="center"><input type="submit" value="上传"></td>
    25      </tr>
    26     </table>
    27   </form>
    28 </body>
    29 </html>

    二:跳转到相应的action  nuc.sw.action---uploadAction.java

     1 package nuc.sw.action;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.util.UUID;
     7 
     8 import com.opensymphony.xwork2.ActionSupport;
     9 
    10 public class uploadAction extends ActionSupport {
    11    private String title;//自定义标题
    12    private File upload; //上传主页面的file名
    13    private String uploadFileName;//实现file的相关联的三个属性
    14    private String uploadContentType;
    15    private String savePath;
    16 public String getTitle() {
    17     return title;
    18 }
    19 public void setTitle(String title) {
    20     this.title = title;
    21 }
    22 public File getUpload() {
    23     return upload;
    24 }
    25 public void setUpload(File upload) {
    26     this.upload = upload;
    27 }
    28 public String getUploadFileName() {
    29     return uploadFileName;
    30 }
    31 public void setUploadFileName(String uploadFileName) {
    32     this.uploadFileName = uploadFileName;
    33 }
    34 public String getUploadContentType() {
    35     return uploadContentType;
    36 }
    37 public void setUploadContentType(String uploadContentType) {
    38     this.uploadContentType = uploadContentType;
    39 }
    40 public String getSavePath() {
    41     return savePath;
    42 }
    43 public void setSavePath(String savePath) {
    44     this.savePath = savePath;
    45 }
    46    
    47 @Override
    48     public String execute() throws Exception {
    //输出5个值,得到相应的理解
    49 System.out.println(title); 50 System.out.println(upload); //临时文件:.tmp 51 System.out.println(uploadFileName); 52 System.out.println(uploadContentType);//文件本质 MIME类型 如:.txt=text/plain 53 System.out.println(savePath); 54 //输入流 55 FileInputStream fis=new FileInputStream(upload); 56 //输出流 57 //上传两次一样的都会成功且显示两个,所以解决覆盖问题 58 FileOutputStream fos=new FileOutputStream(savePath+"/"+UUID.randomUUID().toString()+"_"+uploadFileName); 59 //字节为单位上传 60 byte[] buffer=new byte[1024]; 61 //定义长度 62 int len=0; 63 //从0开始 64 while((len=fis.read(buffer))>0){ 65 fos.write(buffer, 0, len); 66 } 67 //不能写输入输出流 68 return SUCCESS; 69 } 70 }

     

       三:配置struts.xml

          1:  对文件的类型 大小进行限制(实现文件过滤)

          2:设置上传的文件的真实路径  即将.tmp文件保存到savepath文件中

         

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7  <constant name="struts.devMode" value="true" />
     8  <!-- 设置文件的临时保存路径 -->
     9  <constant name="struts.multipart.saveDir" value="d:/tmpsaveDir"></constant>
    10  <package name="default" namespace="/" extends="struts-default">
    11      <action name="uploadAction" class="nuc.sw.action.uploadAction">
    12          <!-- 需要去找源码:struts-default.xml中寻找拦截器  再关联源码 -->
    13          <interceptor-ref name="fileUpload">
    14               <!--设置上传的文件类型 -->
    15                 <!-- 隐藏这一属性后,黑客可以随意上传修改后缀名的病毒文件 (实质没有改变)-->
    16               <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/phpeg,text/plain</param>
    17              <!-- 设置上传的文件的大小 -->
    18              <param name="maximumSize">65535</param>
    19              <!-- 设置上传文件的扩展名 -->
    20              <param name="allowedExtensions">.txt</param>
    21          </interceptor-ref>
    22          <!-- 引用默认拦截器时,需要使用defaultStack -->
    23          <interceptor-ref name="defaultStack"/>
    24          <!-- 设置保存的真实路径 -->
    25          <param name="savePath">d:/uploadRealFile</param>
    26          <result name="success">/hello.jsp</result>
    27          <result name="input">/upload.jsp</result>
    28      </action>
    29  </package>
    30 </struts>

    四:项目结构

     

    五:运行结果

     

  • 相关阅读:
    PAT 字符串-02 删除字符串中的子串
    带滚动条的文本文件
    PAT IO-04 混合类型数据格式化输入(5)
    PAT IO-03 整数均值
    PAT IO-02 整数四则运算
    linux 之shell
    linux 软件包安装-脚本安装
    Linux 关闭防火墙命令
    linux RPM包管理-yum在线管理
    linux 软件包管理
  • 原文地址:https://www.cnblogs.com/Z-D-/p/6053458.html
Copyright © 2011-2022 走看看