• 往服务器上传指定文件



    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.log4j.Logger;

    public class PublicFileUtil
    {
    private static Logger logger = Logger.getLogger(FTPclientUtil.class);

    /**
    * Description: 往服务器上传指定文件 。
    * 1、路径不存在,可以创建路径
    * 2、可以上传指定格式的文件
    *
    * @param remotePath
    * util.properties配制的路径key
    * eg:resource_root.courseware,前面不要系统带环境标示
    * @param fileName
    * 上传到FTP服务器上的文件名
    *
    * @param finput
    * 本地路径 输入流
    *
    * @param isDir
    * false 服务器路径不存在,不创建,直接返回路径不存在(路径有权限控制)
    *
    * @return 成功返回success
    * @throws IOException
    */
    public static String webUploadFile(String remotePath, String fileName,
    InputStream finput, boolean isDir) {
    String result = "uploadError";

    String firstFileName = fileName.substring(0,fileName.lastIndexOf("."));
    String endFileName = fileName.substring(fileName.lastIndexOf("."),fileName.length()).toLowerCase();
    firstFileName=firstFileName+"^"+System.currentTimeMillis();
    fileName = firstFileName+endFileName;
    if (CheckNull.isNull(finput)) {
    // 上传的文件不存在
    return "undefindFile";
    }

    // 判断环境
    String fileSavePath =PublicUtil.getSystemRemotePath(remotePath);

    // 如果文件夹不存在就自动创建一个文件夹
    File fileTemp = new File(fileSavePath);
    if (!(fileTemp.exists()) && !(fileTemp.isDirectory()))
    {
    if(isDir)
    {
    fileTemp.mkdirs();
    }
    else
    {
    //直接返回远程路径不存在
    return "undefindRemort";
    }
    }

    FileOutputStream fos = null;
    boolean falg = true;

    byte[] buffer = new byte[1024];
    int len = 0;
    try
    {
    fos = new FileOutputStream(fileSavePath + File.separator + fileName);
    while ((len = finput.read(buffer)) > 0)
    {
    fos.write(buffer, 0, len);
    }
    fos.flush();
    }
    catch (IOException e)
    {
    logger.error("---上传文件失败!---", e);
    falg = false;
    }

    // 关闭流
    if (null != finput)
    {
    try
    {
    finput.close();
    }
    catch (IOException e)
    {
    logger.error("---上传文件关闭流失败!---", e);
    falg = false;
    }
    }
    if (null != fos)
    {
    try
    {
    fos.close();
    }
    catch (IOException e)
    {
    logger.error("---上传文件关闭流失败!---", e);
    falg = false;
    }
    }

    if (falg)
    {
    result = fileName;
    }
    return result;
    }

    /**
    * 关闭连接
    */
    public static void closeConnect(FTPClient ftpClient, OutputStream os,
    FileInputStream fis,InputStream ins) {
    try {
    if (null != ftpClient) {
    ftpClient.logout();
    ftpClient.disconnect();
    }

    if (null != os) {
    os.flush();
    os.close();
    }

    if (null != fis) {
    fis.close();
    }

    if (null != ins) {
    ins.close();
    }

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    android TextView字体设置最少占多少行. 及其 Java String 字符串操作 . .
    Android ViewSwitcher 的功能与用法
    (14):Chain of Responsibility 职责链模式(行为型模式)
    享元模式Flyweight
    外观模式Facade
    装饰模式(Decorator)
    软件实现
    面向对象设计
    面向对象分析
    面向对象基础
  • 原文地址:https://www.cnblogs.com/dingding0505/p/4208793.html
Copyright © 2020-2023  润新知