文件的下载和上传

文件的上传和下载用普通方法的话,会较麻烦

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

public class MyUpFile {
       private File file;
       //获取文件的名字 xxxFileName
       private  String fileFileName;
       //获取文件的类型 xxxContentType
       private String fileContentType;

    private String downName;

    //文件的上传
       public String upFile(){
           System.out.println(fileFileName);
           //得到当前文件的后缀
           String newName=fileFileName.substring(fileFileName.lastIndexOf("."));
           newName=System.currentTimeMillis()+newName;
           System.out.println(fileContentType);
           //获取项目在服务器中的绝对路径
           String path=ServletActionContext.getServletContext().getRealPath("/");
           System.out.println(path);
           try {
             //把文件读成输入流
            InputStream ips =new FileInputStream(file);
            //输出到什么位置
            OutputStream ops=new FileOutputStream(path+"upFile/"+fileFileName);//newName这个是以当前毫秒数当成是名字 
            byte[] b=new byte[1024*20];
            int num=ips.read(b);
            while(num!=-1){
                ops.write(b, 0, num);
                num=ips.read(b);
            }
            ops.close();
            ips.close();
           } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           return "success";
       }


       //文件的下载
       public String down(){
           try {
            downName=new String(downName.getBytes("iso-8859-1"),"utf-8");
            //告诉浏览器是我是来下载
            ServletActionContext.getResponse().setContentType("application/x-msdownload");
            //弹出保存窗体
            ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename="+downName);
            //获取项目在服务器中的绝对路径
            String path=ServletActionContext.getServletContext().getRealPath("/");
            path=path+"upFile/"+downName;
            //把文件读成输入流
            InputStream ips =new FileInputStream(path);
            //得到响应的输出流
            OutputStream ops=ServletActionContext.getResponse().getOutputStream();
            byte[] b=new byte[1024*20];
            int num=ips.read(b);
            while(num!=-1){
                ops.write(b, 0, num);
                num=ips.read(b);
            }
            ops.close();
            ips.close();
          } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          //下载不需要跳转页面
           return null;
       }
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
    public String getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
    public String getDownName() {
        return downName;
    }
    public void setDownName(String downName) {
        this.downName = downName;
    }


}

index.jsp页面

  <body>
    -------------传统-------
  <!-- 一定是post ;enctype="multipart/form-data"-->
    <form action="myUp!upFile" method="post" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="submit"/>
    </form>
    -----------------------------
    <a href="myUp!down?downName=1.jpg">下载1.jpg</a>
    </body>

sturuts.xml文件的配置

<!-- 设置上传的大小  默认是2M-->
<constant name="struts.multipart.maxSize" value="2097152*2"></constant>
    <package name="ss" namespace="/" extends="struts-default">
        <action name="myUp" class="action.MyUpFile">
            <result>index.jsp</result>
        </action>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值