前言
最近在著手語音識別系統(tǒng),大致的功能是將語音轉(zhuǎn)成文本后對文本內(nèi)容進(jìn)行一系列的分析。在文本分析的過程中,需要調(diào)用公司另一個哥們提供的python接口,分析過程生成的圖片是保存在他自己的電腦上并將這個圖片地址保存到數(shù)據(jù)庫。由于我本地不存在該圖片,所以頁面請求訪問不到圖片的資源。于是,我打算在中間另加一道,遠(yuǎn)程讀取他電腦上的圖片文件并下載至我本地項目設(shè)置好的目錄下。
注: 在讀取遠(yuǎn)程文件的過程中,確保文件所在的文件夾是共享的。
代碼示例
自己在用的代碼我也懶的修改了,就直接貼上來了。
/**
* 遠(yuǎn)程下載圖片至本地目錄
* 僅解決目前 圖片 和 程序 存儲在不同服務(wù)器上的問題
* 后期做修改
* Created by zhh on 2017/11/14.
*/
public class ImagesDownloadUtils {
private static Logger logger = LoggerFactory.getLogger(ImagesDownloadUtils.class);
// 格式: smb://用戶名:密碼@機(jī)器ip/
private static final String SMB_ROOT_PATH = "smb://Administrator:nlpqs@ip/";
/**
* 遠(yuǎn)程下載圖片至本地目錄
* @param remotePath 遠(yuǎn)程文件路徑 例如:E:/WordCloudImgs/t7_1510645760.png
* @return
*/
@SuppressWarnings("unused")
public static String smbGet(String remotePath) {
InputStream in = null;
OutputStream out = null;
try {
// 剔除遠(yuǎn)程文件路徑的根磁盤路徑
logger.info("共享文件路徑: " + remotePath);
// 去除磁盤根目錄, E:/WordCloudImgs/t7_1510645760.png -> WordCloudImgs/t7_1510645760.png
remotePath = remotePath.substring(3);
// 拼接遠(yuǎn)程共享文件路徑
remotePath = SMB_ROOT_PATH + remotePath;
logger.info("文件遠(yuǎn)程路徑: " + remotePath);
// 本地存儲路徑(自行設(shè)置本機(jī)需要保存文件的路徑)
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("遠(yuǎn)程連接異常!");
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
以上代碼訪問同事的機(jī)器是沒什么問題的,為了簡單起見把python服務(wù)移到了公司內(nèi)部的服務(wù)器上。本以為改下用戶、密碼和IP就可以用了,但是好像想的太簡單了。
提示禁用明文密碼。后來我在以上代碼當(dāng)中加入了這樣一行代碼。
......
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錯誤表 也沒什么收獲。
然后,換種思路,去遠(yuǎn)程查看了下服務(wù)器的系統(tǒng)日志,是以下這個樣子的。
服務(wù)器無法通過系統(tǒng)非頁面共享區(qū)來進(jìn)行分配,因為服務(wù)器已達(dá)非頁面共享分配的配置極限。 錯誤原因找到了。按照以下方式修改了服務(wù)器的注冊表(撐死膽大的,餓死膽小的)。
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management] "LargeSystemCache"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters]
"Size"=dword:00000003
。
修改好注冊表上述內(nèi)容之后,重啟下服務(wù)器。再次進(jìn)行嘗試,遠(yuǎn)程文件讀取成功!
|