• Struts文件上传下载


    Struts配置文件:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <constant name="struts.devMode" value="true" />
        <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
        <package name="default" extends="struts-default">
            <action name="upload" class="com.itheima.action.UploadAction" method="upload">
                <result>/1.jsp</result>
                <result name="input">/upload.jsp</result>
                <result name="error">/error.jsp</result>
            </action>
            <action name="download" class="com.itheima.action.UploadAction" method="download">
                <result type="stream">
                    <param name="contentType">application/octet-stream</param>
                    <!-- 在配置文件中使用OGNL表达式 -->
                    <param name="contentDisposition">attachment;filename=${@java.net.URLEncoder@encode(photoFileName,'UTF-8')}</param>
                    <param name="inputName">myInputStream</param>
                </result>
            </action>
        </package>
    
    
    </struts>

    action方法:

     1 package com.itheima.action;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.InputStream;
     8 import java.io.OutputStream;
     9 
    10 import org.apache.struts2.ServletActionContext;
    11 
    12 import com.opensymphony.xwork2.ActionSupport;
    13 
    14 public class UploadAction extends ActionSupport {
    15     private File photo;
    16     private String photoFileName;
    17     private String photoContentType;
    18     private InputStream myInputStream;
    19     
    20     
    21     public InputStream getMyInputStream() {
    22         return myInputStream;
    23     }
    24 
    25     public void setMyInputStream(InputStream myInputStream) {
    26         this.myInputStream = myInputStream;
    27     }
    28 
    29     public File getPhoto() {
    30         return photo;
    31     }
    32 
    33     public void setPhoto(File photo) {
    34         this.photo = photo;
    35     }
    36 
    37     public String getPhotoFileName() {
    38         return photoFileName;
    39     }
    40 
    41     public void setPhotoFileName(String photoFileName) {
    42         this.photoFileName = photoFileName;
    43     }
    44 
    45     public String getPhotoContentType() {
    46         return photoContentType;
    47     }
    48 
    49     public void setPhotoContentType(String photoContentType) {
    50         this.photoContentType = photoContentType;
    51     }
    52 
    53     public String download() throws FileNotFoundException{
    54         photoFileName = "整容广告.jpg";
    55         //就是myInputStream赋值
    56         String storeDirectory = ServletActionContext.getServletContext().getRealPath("/files/整容广告.jpg");
    57         myInputStream = new FileInputStream(storeDirectory);
    58     
    59         return SUCCESS;
    60     }
    61     public String upload(){
    62         try {
    63             InputStream in = new FileInputStream(photo);
    64             String storeDirectory = ServletActionContext.getServletContext().getRealPath("/files");
    65             OutputStream out = new FileOutputStream(storeDirectory+"/"+photoFileName);
    66             int len = -1;
    67             byte b[] = new byte[1024];
    68             while((len=in.read(b))!=-1){
    69                 out.write(b, 0, len);
    70             }
    71             in.close();
    72             out.close();
    73             return SUCCESS;
    74         }  catch (Exception e) {
    75             e.printStackTrace();
    76             return ERROR;
    77         }
    78     }
    79 }

    jsp页面:

        <s:form action="upload" enctype="multipart/form-data">
            <s:file label="靓照" name="photo"></s:file>
            <s:submit value="上传"></s:submit>
        </s:form>
  • 相关阅读:
    better-scroll 介绍
    promise 异步编程
    vue网址路由的实时检测
    浏览器本地存储的使用
    获取元素的位置
    如何设置动画的运动效果
    实现对称加密及非对称公钥加密
    Centos 7系统启动修复
    Centos 7服务启动文件
    内核编译-4.12
  • 原文地址:https://www.cnblogs.com/lzy1991/p/5351174.html
Copyright © 2020-2023  润新知