开源OA教程(O2OA):Ftp文件上传方法使用说明

news/2024/7/20 8:04:47 标签: java, github, android, ios, 办公软件

FTP上传功能因不是平台自带功能,所以需要进行额外的java开发。当把jar包打包后,需要放入到O2目录:o2server\custom\jars。

重启服务器后生效!

一、自定义包DocumentManager.java

package com.z.custom;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;

public class DocumentManager {
    private static HttpServletRequest request;
    private static FileSystemManager fsManager = null;

    static {
        try {
            fsManager = VFS.getManager();
        } catch (FileSystemException e) {
            System.out.println(e.getMessage());
        }
    }
    

    public static byte[] readFileToByte(String filePath, String encoding) throws IOException {
        if (StringUtils.isEmpty(filePath)) {
            throw new IOException("File '" + filePath + "' is empty.");
        }
        FileObject fileObj = null;
        InputStream in = null;
        try {
            fileObj = fsManager.resolveFile(filePath);
            if (fileObj.exists()) {
                if (org.apache.commons.vfs2.FileType.FOLDER.equals(fileObj.getType())) {
                    throw new IOException("File '" + filePath + "' exists but is a directory");
                } else {
                    in = fileObj.getContent().getInputStream();
                    return IOUtils.toByteArray(in);
                }
            } else {
                throw new FileNotFoundException("File '" + filePath + "' does not exist");
            }
        } catch (FileSystemException e) {
            throw new IOException("File '" + filePath + "' resolveFile fail.");
        } finally {
            in.close();
            IOUtils.closeQuietly(in);
            if (fileObj != null) {
                fileObj.close();
            }
        }
    }
    
   
    public static void writeByteToFile(String filePath, byte[] fileByte, String encoding) throws IOException {
        if (StringUtils.isEmpty(filePath)) {
            throw new IOException("File '" + filePath + "' is empty.");
        }
        FileObject fileObj = null;
        OutputStream out = null;
        filePath = new String(filePath.getBytes("UTF-8"),"ISO-8859-1");

        try {
            fileObj = fsManager.resolveFile(filePath);

            if (!fileObj.exists()) {
                fileObj.createFile();
            } else {
                if (org.apache.commons.vfs2.FileType.FOLDER.equals(fileObj.getType())) {
                    throw new IOException("Write fail. File '" + filePath + "' exists but is a directory");
                }
            }

            out = fileObj.getContent().getOutputStream();
            IOUtils.write(fileByte, out);

        } catch (FileSystemException e) {
            throw new IOException("File '" + filePath + "' resolveFile fail.");
        } finally {
            out.flush();
            out.close();
            IOUtils.closeQuietly(out);
            if (fileObj != null) {
                fileObj.close();
            }
        }
    }

}

二、服务管理平台创建接口编写上传ftp接口代码

1、打开服务管理平台

服务管理平台.png

2、创建接口写上相关代码

创建接口.png

FTP上传.png

/*
* resources.getEntityManagerContainer() // 实体管理容器.
* resources.getContext() //上下文根.
* resources.getOrganization() //组织访问接口.
* requestText //请求内容.
* request //请求对象.
*/


try{
    var result = {
       
    }

    //FTP信息配置
    var FTPhost = "172.16.92.23"; //FTP服务器IP
    var FTPport = 21;   //FTP服务器端口
    var FTPuserName = "wwx";    //FTP服务器登录用户名
    var FTPpassword = "wwx";    //FTP服务器登录密码

    var FTPpath = "upload";     //存放文件的目录       
    var filePath = "D:\\测试.docx"; //需上传的文件

    var documentManager = Java.type('com.z.custom.DocumentManager'); //实例化java类
    
    strpath = "ftp://"+FTPuserName+":"+FTPpassword+"@"+FTPhost+"/"+FTPpath+"/测试.docx";
    //调用方法进行上传
    documentManager.writeByteToFile(strpath, documentManager.readFileToByte(filePath,"utf-8"), "utf-8");

    result.state = "NMT0001";
    result.message = "成功";
    
}catch(e){
    e.printStackTrace();
    result.state = "NMT0002";
    result.message = "失败";
    result.data = e.name + ": " + e.message
}
//JSON.stringify(result);
this.response.setBody(result,"application/json");

官方网站及相关资源:

开源主页 : https://www.oschina.net/p/o2oa

官方网站 : http://www.o2oa.net

Gitee : https://gitee.com/o2oa/O2OA

Github : https://github.com/o2oa/o2oa

脚本API:http://www.o2oa.net/x_desktop/portal.html?id=dcd8e168-2da0-4496-83ee-137dc976c7f6

O2OA开发相关教程天梯:https://my.oschina.net/o2oa/blog/3016363

O2OA语雀教程:https://www.yuque.com/o2oa/course

官方论坛:http://forum.o2oa.net/x_deskto


http://www.niftyadmin.cn/n/953328.html

相关文章

解答:OA办公系统中的内容管理是什么?怎么样?

在办公时,公司内部总会有最新动态的推送、各种通知、考勤数据、各类文件更新等等,有时候找这些信息所在的位置,会浪费不少的时间。但是在办公OA系统中嵌入内容管理平台,就可以将这些零散的信息进行新的整合,使得文档数…

开源OA办公平台教程(O2OA):WebService调用样例

一、接口样例 1、打开服务管理平台 2、创建接口写上相关代码 /******************** this.entityManager; //实体管理器 this.applications; //访问系统内服务 this.requestText//请求正文 this.request//请求 this.currentPerson//当前用户 this.response//响应对象。通过thi…

开源办公软件:移动端会议管理功能上线!

企业会议区分多而杂,从参会人员来说:分为高层会议、部门会议、小组会议、全体成员会议等;从会议周期来看:分为每日例会、每周例会、月度会议等;还有从事务角度看:分为总结大会、动员大会等等。 这样许许多…

开源OA搭建教程:腾讯企业邮件接入O2平台

一、 功能介绍 企业注册了腾讯企业邮箱,又用o2平台情况下可以把腾讯企业邮箱集成到o2平台,用门户组件开发应用显示腾讯企业邮件收件箱的邮件,以及新邮件数量提醒。并且平台提供了SSO服务,可以直接通过平台跳转到腾讯企业邮件页面…

公司员工考勤问题应当如何解决?

企业中的考勤问题一般有以下几点: 考勤申请复杂:请假单、加班单、出差单等申请都是纸质流通,难以保存,程序麻烦。 考勤数据繁琐:考勤需要人力采集大量的数据,然后进行计算、汇总、审批上报。 考勤流于形…

开源OA办公平台教程:手机APP指纹认证的配置

一、 功能介绍 通过SSO的配置完成手机APP的指纹或者人脸识别登录。 二、 手机的设置前提 在手机设置中开启手机的指纹解锁或者人脸解锁。现在安卓暂时只支持支持指纹识别登录,IOS支持指纹和人脸识别,不过IOS手机本身一般也只具有指纹或者人脸识别功能…

利用开源OA办公系统,进行工作管理

在我们的日常工作中,一般都是将一整堆工作排在一段指定时间之内完成,但是这样一来,许多人的“拖延症”会对工作效率产生负面影响,一些临时调度的任务也无法很好完成。 工作管理功能,是一款能够将工作任务全面结构化、…

浅谈:企业发展和内容管理有何联系?

随着互联网技术的发展,网页的信息衍生快速繁多,整理和储存这些大量的内容数据、深入挖掘这些数据的价值,不仅会耗费大量的人力物力财力,还会降低整体工作效率,减弱知识资产的量化能力,但是OA系统中的内容管…