博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
上传文件
阅读量:7264 次
发布时间:2019-06-29

本文共 3644 字,大约阅读时间需要 12 分钟。

hot3.png

package com.lanou.strusUpdate;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class uploadAction extends ActionSupport {

    File[] liguocheng;
    String[]  liguochengFileName;
    InputStream is;
    
    public String upload() throws IOException{
        
    
        /*try {
            FileInputStream fis=new FileInputStream(liguocheng);
            String path=ServletActionContext.getServletContext().getRealPath("/upload/");
            System.out.println(path);
            FileOutputStream fos=new FileOutputStream(path+"/"+liguochengFileName,true);
            byte[] b=new byte[1024];
            int l=-1;
            while(fis.read(b)>0){
                fos.write(b);
            }
            fos.close();
            fis.close();
        
        } catch (FileNotFoundException e) {
        
            e.printStackTrace();
        } catch (IOException e) {
            
            e.printStackTrace();
        }*/    
        for(int i=0;i<liguocheng.length;i++){
            String path=ServletActionContext.getServletContext().getRealPath("/upload/");
            FileUtils.copyFile(liguocheng[i], new File(path+"/"+liguochengFileName[i]));
        }
    
        
        /*int a=liguochengFileName.lastIndexOf(".");
        String strname1= liguochengFileName.substring(0,a);
        String strname2=liguochengFileName.substring(a);*/
        //String newPath=strname1+System.currentTimeMillis()+strname2;
        
        
        
        
        System.out.println(System.currentTimeMillis());
        return SUCCESS;
    }
    
    public String download() throws FileNotFoundException{
        String path=ServletActionContext.getServletContext().getRealPath("/upload/");
        is=new FileInputStream(path+"/"+"1.jpg");
        String str="pc.jpg";
        ActionContext.getContext().getValueStack().set("down", str);
        return SUCCESS;
    }

    public File[] getLiguocheng() {

        return liguocheng;
    }

    public void setLiguocheng(File[] liguocheng) {

        this.liguocheng = liguocheng;
    }

    public String[] getLiguochengFileName() {

        return liguochengFileName;
    }

    public void setLiguochengFileName(String[] liguochengFileName) {

        this.liguochengFileName = liguochengFileName;
    }

    public InputStream getIs() {

        return is;
    }

    public void setIs(InputStream is) {

        this.is = is;
    }

}

 

 

<?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.enable.DynamicMethodInvocation" value="false" />

    <!--ture代表启动 false停止 -->
    <constant name="struts.devMode" value="ture" />

    <package name="default" namespace="/" extends="json-default">

        <action name="fileupload" class="com.lanou.strusUpdate.uploadAction"
            method="upload">
            <result name="success">
                seccess.html
            </result>
        </action>
        
        <action name="fileDownload" class="com.lanou.strusUpdate.uploadAction"
            method="download">
            <result name="success" type="stream">
                <param name="inputName">is</param>
                <param name="contentDisposition">attachment;filename=${down}</param>
            </result>
            
        </action>
    </package>
    
</struts>

 

 

 

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

   <form action="fileupload"  method="post" enctype="multipart/form-data">

         请选择要上传的文件: <input type="file" name="liguocheng">
         <br>
           请选择要上传的文件: <input type="file" name="liguocheng">
           <br>
             请选择要上传的文件: <input type="file" name="liguocheng">
             <br>
               请选择要上传的文件: <input type="file" name="liguocheng">
               <br>
                 请选择要上传的文件: <input type="file" name="liguocheng">
   <input type="submit"> 
   
   </form>
</body>
</html>

 

 

 

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   成功  <a href="fileDownload">下载文件</a>
</body>
 

转载于:https://my.oschina.net/18829297883/blog/1305842

你可能感兴趣的文章
WebStorm 2017.1增加对Vue.js的支持
查看>>
“软”苹果水逆的一周:杂志服务崩溃,新机型遭泄露,芯片首架离职
查看>>
红帽收购混合云管理提供商NooBaa,混合云爆发节点临近!
查看>>
腾讯云自主可控数据库TDSQL的架构演进\n
查看>>
时隔25年重访 Linus:流量时代的“技术大师”
查看>>
.NET特性:异步流
查看>>
BitKeeper 7.3发布,新增从Git快速导入等功能
查看>>
GitHub使用Spokes进行跨数据中心复制
查看>>
Kubernetes 1.10和Cross-Cloud CI Project Dashboard 1.3释放,Kubernetes发布用户调查
查看>>
Spark灰度发布在十万级节点上的实践
查看>>
Facebook何恺明团队提出SlowFast网络,视频识别无需预训练
查看>>
TiKV 源码解析系列——如何使用 Raft
查看>>
全局角度出发讨论敏捷
查看>>
MySQL 8的关键新特性
查看>>
VS 2019要来了,是时候了解一下C# 8.0新功能
查看>>
ChaosConf 2018:混沌实验的演变
查看>>
2016 在路上
查看>>
对MySQL进行逻辑卷备份与恢复
查看>>
深入Atlas系列:Web Sevices Access in Atlas(2) - 服务器端支持(上)
查看>>
网络隔离下的几种数据交换技术比较
查看>>