前言
最近在着手语音识别系统,大致的功能是将语音转成文本后对文本内容进行一系列的分析。在文本分析的过程中,需要调用公司另一个哥们提供的python接口,分析过程生成的图片是保存在他自己的电脑上并将这个图片地址保存到数据库。由于我本地不存在该图片,所以页面请求访问不到图片的资源。于是,我打算在中间另加一道,远程读取他电脑上的图片文件并下载至我本地项目设置好的目录下。
注: 在读取远程文件的过程中,确保文件所在的文件夹是共享的。
代码示例
自己在用的代码我也懒的修改了,就直接贴上来了。
/**
* 远程下载图片至本地目录
* 仅解决目前 图片 和 程序 存储在不同服务器上的问题
* 后期做修改
* Created by zhh on 2017/11/14.
*/
public class ImagesDownloadUtils {
private static Logger logger = LoggerFactory.getLogger(ImagesDownloadUtils.class);
// 格式: smb://用户名:密码@机器ip/
private static final String SMB_ROOT_PATH = "smb://Administrator:nlpqs@ip/";
/**
* 远程下载图片至本地目录
* @param remotePath 远程文件路径 例如:E:/WordCloudImgs/t7_1510645760.png
* @return
*/
@SuppressWarnings("unused")
public static String smbGet(String remotePath) {
InputStream in = null;
OutputStream out = null;
try {
// 剔除远程文件路径的根磁盘路径
logger.info("共享文件路径: " + remotePath);
// 去除磁盘根目录, E:/WordCloudImgs/t7_1510645760.png -> WordCloudImgs/t7_1510645760.png
remotePath = remotePath.substring(3);
// 拼接远程共享文件路径
remotePath = SMB_ROOT_PATH + remotePath;
logger.info("文件远程路径: " + remotePath);
// 本地存储路径(自行设置本机需要保存文件的路径)
String localPath = MultipartFileOperation.createFolderByDate(ProjectConstant.FilePath.IMAGES);
SmbFile remoteFile = new SmbFile(remotePath);
if (remoteFile == null) {
logger.info("共享文件不存在, 共享文件路径为: " + remotePath);
return null;
}
String fileName = remoteFile.getName();
String localFilePath = localPath + fileName;
File localFile = new File(localFilePath);
if (localFile.exists()) {
logger.info("本地已存在该共享文件! 文件路径为: " + localFilePath);
}
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
logger.info("共享文件下载成功!");
logger.info("本地存储共享文件路径:" + localFilePath);
return localFilePath;
} catch (Exception e) {
e.printStackTrace();
logger.info("远程连接异常!");
return null;
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
以上代码访问同事的机器是没什么问题的,为了简单起见把python服务移到了公司内部的服务器上。本以为改下用户、密码和IP就可以用了,但是好像想的太简单了。
提示禁用明文密码。后来我在以上代码当中加入了这样一行代码。
......
String localPath = MultipartFileOperation.createFolderByDate(ProjectConstant.FilePath.IMAGES);
// 过滤 禁用明文密码 异常
jcifs.Config.setProperty("jcifs.smb.client.disablePlainTextPasswords","false");
SmbFile remoteFile = new SmbFile(remotePath);
if (remoteFile == null) {
logger.info("共享文件不存在, 共享文件路径为: " + remotePath);
return null;
}
......
于是又引出了另一个问题:
于是就卡在了这个地方,谷歌了半天好像也没有什么特别好的办法,去找了下 SMB错误表 也没什么收获。
然后,换种思路,去远程查看了下服务器的系统日志,是以下这个样子的。
服务器无法通过系统非页面共享区来进行分配,因为服务器已达非页面共享分配的配置极限。 错误原因找到了。按照以下方式修改了服务器的注册表(撑死胆大的,饿死胆小的)。
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management] "LargeSystemCache"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters]
"Size"=dword:00000003
。
修改好注册表上述内容之后,重启下服务器。再次进行尝试,远程文件读取成功!
|