码迷,mamicode.com
首页 > Web开发 > 详细

文件上传和下载

时间:2020-03-12 23:31:18      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:font   size   bsp   stack   bst   读取数据   apach   osi   int   

import org.apache.struts2.ServletActionContext;

1、上传文件

static boolean isWindows = false;
private String uploadDir;
private File uploadify;           //上传文件file对象
private String uploadifyFileName; //上传文件名
private String load;

/** return true---是Windows操作系统 */
public static boolean isWindowsOS() {
    boolean isWindowsOS = false;
    String osName = System.getProperty("os.name");
    if (osName.toLowerCase().indexOf("windows") > -1) {
        isWindowsOS = true;
    }
    return isWindowsOS;
}
//上传文件
public String upload(){  
    String newFileName=null;  
    //得到当前时间开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名  
    long now=new Date().getTime();  
    //得到保存上传文件的真实路径  
    isWindows = isWindowsOS(); // 判断是否是windows系统
    String path="";
    if (isWindows) {// windows
        uploadDir = "/upload/contrast";
        path=ServletActionContext.getServletContext().getRealPath(uploadDir);
    } else {// linux
        uploadDir = "/data/upload/contrast";
        path = uploadDir;
    }
    
    File dir=new File(path);  
    if (!dir.exists()) {    //如果这个目录不存在,则创建它 
        dir.mkdirs(); 
    }  
    int index=uploadifyFileName.lastIndexOf(".");  
    //判断上传文件是否有扩展名,以时间戳作为新的文件名  
    if (index!=-1) {  
        newFileName=now+uploadifyFileName.substring(index); 
        load=uploadDir+"/"+newFileName;
    }else {  
        newFileName=Long.toString(now);  
    }  
    
    BufferedOutputStream bos=null;  
    BufferedInputStream bis=null;  
          
    //读取保存在临时目录下的上传文件,写入到新的文件中  
    try {  
        //File f = new File("C:/java/hello");
        //InputStream out = new FileInputStream(f);   //该流用于从文件读取数据
        
        FileInputStream fis=new FileInputStream(uploadify);  
        bis=new BufferedInputStream(fis);  
          
        FileOutputStream fos=new FileOutputStream(new File(dir,newFileName));  
        bos=new BufferedOutputStream(fos);  
          
        byte [] buf=new byte[4096];  
        int len=-1;  
        while ((len=bis.read(buf))!=-1) {  
            bos.write(buf,0,len);  
        }  
    } catch (FileNotFoundException e) {  
         e.printStackTrace();  
    } catch (IOException e) {  
         e.printStackTrace();  
    }finally{  
        if (null!=bis) {  
            try {  
                bis.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        if (null!=bos) {  
            try {  
                bos.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    return null;  
}  

 

2、下载文件

//附件下载 
public String download() {
    try {
        String path="";
        String realPath  = "/upload/contrast/test.java"; 
        realPath = realPath.replace(‘\\‘, ‘/‘);
        isWindows = isWindowsOS(); // 判断是否是windows系统
        if (isWindows) {// windows
            path=ServletActionContext.getServletContext().getRealPath(realPath);
        } else {// linux
            path=realPath;
        }
        String load = path;
         
        File file = new File(load);
        InputStream inputStream = new BufferedInputStream(new FileInputStream(load));
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        Utils.getResponse().reset();
        String fileName = daoHelper.queryForObject("product.gms.bom.bomCheck.findfileVisteName", this).toString();
        Utils.getResponse().setCharacterEncoding("UTF-8");
        Utils.getResponse().setContentType("application/txt");
        Utils.getResponse().addHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
        Utils.getResponse().addHeader("Content-Length", "" + file.length());
        OutputStream outputStream = new BufferedOutputStream(Utils.getResponse().getOutputStream());
        outputStream.write(buffer);
        inputStream.close();
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

 

文件上传和下载

标签:font   size   bsp   stack   bst   读取数据   apach   osi   int   

原文地址:https://www.cnblogs.com/leilei-y/p/12483432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!