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