ftp上传功能因不是平台自带功能,所以需要进行额外的java开发。当把jar包打包后,需要放入到o2目录:o2servercustomjars。
重启服务器后生效!
package com.z.custom; import java.io.*; import java.io.filenotfoundexception; import java.util.date; import java.util.objects; import javax.servlet.http.httpservletrequest; import com.x.base.core.entity.storageprotocol; import com.x.base.core.project.config.config; import com.x.base.core.project.tools.defaultcharset; import org.apache.commons.io.ioutils; import org.apache.commons.lang3.stringutils; import org.apache.commons.vfs2.*; import org.apache.commons.vfs2.provider.ftp.ftpfilesystemconfigbuilder; import org.apache.commons.vfs2.provider.ftp.ftpfiletype; 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, boolean passive) throws exception { 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 { filesystemoptions opts = new filesystemoptions(); ftpfilesystemconfigbuilder ftpbuilder = ftpfilesystemconfigbuilder.getinstance(); ftpbuilder.setpassivemode(opts, passive); /** 强制不校验ip */ ftpbuilder.setremoteverification(opts, false); ftpbuilder.setfiletype(opts, ftpfiletype.binary); ftpbuilder.setconnecttimeout(opts, 10000); ftpbuilder.setsotimeout(opts, 10000); ftpbuilder.setcontrolencoding(opts, defaultcharset.name); try (fileobject fo = fsmanager.resolvefile(filepath, opts); outputstream output = fo.getcontent().getoutputstream()) { ioutils.copylarge(new bytearrayinputstream(filebyte), output); } } catch (exception e) { system.out.println(e.getmessage()); throw e; } } }
/* * 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 filename = "测试.docx"; var filepath = "d:" filename; //需上传的文件 var documentmanager = java.type('com.z.custom.documentmanager'); //实例化java类 strpath = "ftp://" ftpusername ":" ftppassword "@" ftphost "/" ftppath "/" filename; //调用方法进行上传 documentmanager.writebytetofile(strpath, documentmanager.readfiletobyte(filepath,"utf-8"), "utf-8",true); result.type = "success"; result.message = "成功"; }catch(e){ e.printstacktrace(); result.type = "error"; result.message = "失败"; result.data = e.name ": " e.message } //json.stringify(result); this.response.setbody(result,"application/json"); 1 人点赞1
下一篇:藕粉社区问答系列1