zoukankan      html  css  js  c++  java
  • smb上传图片工具类

    package com.zfq.springcloud.controller;

    import jcifs.smb.NtlmPasswordAuthentication;
    import jcifs.smb.SmbFile;
    import jcifs.smb.SmbFileInputStream;
    import jcifs.smb.SmbFileOutputStream;
    import org.springframework.web.multipart.MultipartFile;

    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;

    /**
    * @descriptions: 上传共享文件夹
    * @author: FUQIANG.ZHOU
    * @date: 2021/2/3 23:09
    * @version: 1.0
    */
    public class SmbUtil {


    /** 文件上传
    * String localFilePath:要上传的本地文件路径
    * String path:远程服务器共享文件夹名称
    * String username:远程服务器用户名
    * String password:远程服务器密码
    * */
    public static void upload(MultipartFile localFile, String path, String username, String password) {
    OutputStream out = null;
    try (InputStream in = localFile.getInputStream()) {

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
    String remoteUrl = "smb:" + path + (path.endsWith(File.separator) ? "" : File.separator);
    SmbFile remoteFile = new SmbFile(remoteUrl + File.separator + localFile.getName(), auth);
    remoteFile.connect();
    out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
    byte[] buffer = new byte[4096];
    int len = 0;
    while ((len = in.read(buffer, 0, buffer.length)) != -1) {
    out.write(buffer, 0, len);
    }
    out.flush();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    if (out != null) {
    out.close();
    }
    } catch (Exception e) {
    }
    }
    }

    private void responseFileStream(HttpServletResponse response, String filePath){
    ServletOutputStream out = null;
    FileInputStream in = null;
    try {

    in = new FileInputStream(new File(filePath));
    String[] dir = filePath.split("/");
    String fileName = dir[dir.length-1];
    String[] array = fileName.split("[.]");
    String fileType = array[array.length-1].toLowerCase();
    //设置文件ContentType类型//图片类型
    if("jpg,jepg,gif,png".contains(fileType)){
    response.setContentType("image/"+fileType);
    //pdf类型
    }else if("pdf".contains(fileType)){
    response.setContentType("application/pdf");
    }else{//自动判断下载文件类型
    response.setContentType("multipart/form-data");
    }
    //设置文件头:最后一个参数是设置下载文件名
    response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
    out = response.getOutputStream();
    // 读取文件流
    int len = 0;
    byte[] buffer = new byte[1024 * 10];
    while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
    }
    out.flush();
    } catch (FileNotFoundException e) {
    } catch (Exception e) {
    } finally {
    try {
    out.close();
    in.close();
    } catch (NullPointerException e) {
    } catch (Exception e) {
    }
    }
    }

    /**
    * 文件下载
    * String localFilePath:本地文件夹
    * String path:远程服务器共享文件名
    * String username:远程服务器用户名
    * String password:远程服务器密码
    */
    public static void download2(HttpServletResponse response, String path, String username, String password) {
    InputStream in = null;
    try (OutputStream out = response.getOutputStream()){
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
    String remoteUrl = "smb:" + path + (path.endsWith("/") ? "" : "/");
    SmbFile remoteFile = new SmbFile(remoteUrl, auth);
    remoteFile.connect();
    in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
    byte[] buffer = new byte[4096];
    int len = 0;
    while ((len = in.read(buffer, 0, buffer.length)) != -1) {
    out.write(buffer, 0, len);
    }
    out.flush();
    } catch (Exception e) {
    throw new RuntimeException(e.getMessage());
    } finally {
    try {
    if (in != null) {
    in.close();
    }
    } catch (Exception e) {
    throw new RuntimeException(e.getMessage());
    }
    }
    }

    public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    System.out.println(now.format(dtf));
    //LocalDateTime localDateTime = new LocalDateTime;
    // localDateTime.format("yyyyMMddHHmmss");
    }

    }

  • 相关阅读:
    PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
    TP5安装workerman版本的坑
    下载git2.2.1并将git添加到环境变量中
    RedHat安装git报错 expected specifier-qualifier-list before ‘z_stream’
    Git出现fatal: Unable to find remote helper for 'https'
    ThinkPHP5实现定时任务
    php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期
    git 查看日志记录
    程序员必读之软件架构 读书笔记
    centos7 安装桌面
  • 原文地址:https://www.cnblogs.com/fuqiang-zhou/p/14370546.html
Copyright © 2011-2022 走看看