0
我有一個Java程序調用我的Perl腳本來上傳文件。它有一個Perl腳本的文件參數,它包含要上傳的文件的位置。perl文件句柄不讀取名稱中包含空格的文件
public static void legacyPerlInspectionUpload(String creator, String artifactId, java.io.File uploadedFile, String description) {
PostMethod mPost = new PostMethod(getProperty(Constants.PERL_FILE_URL) + "inspectionUpload.pl");
try {
String upsSessionId = getUpsSessionCookie();
//When passing multiple cookies as a String, seperate each cookie with a semi-colon and space
String cookies = "UPS_SESSION=" + upsSessionId;
log.debug(getCurrentUser() + " Inspection File Upload Cookies " + cookies);
Part[] parts = {
new StringPart("creator", creator),
new StringPart("artifactId", artifactId),
new StringPart("fileName", uploadedFile.getName()),
new StringPart("description", description),
new FilePart("fileContent", uploadedFile) };
mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost.getParams()));
mPost.setRequestHeader("Cookie",cookies);
HttpClient httpClient = new HttpClient();
int status = httpClient.executeMethod(mPost);
if (status == HttpStatus.SC_OK) {
String tmpRetVal = mPost.getResponseBodyAsString();
log.info(getCurrentUser() + ":Inspection Upload complete, response=" + tmpRetVal);
} else {
log.info(getCurrentUser() + ":Inspection Upload failed, response=" + HttpStatus.getStatusText(status));
}
} catch (Exception ex) {
log.error(getCurrentUser() + ": Error in Inspection upload reason:" + ex.getMessage());
ex.printStackTrace();
} finally {
mPost.releaseConnection();
}
}
在我的Perl腳本的一部分,它獲取有關文件的信息,從中讀取和寫入我的服務器中的內容到一個眨眼的文件。
#
# Time to upload the file onto the server in an appropropriate path.
#
$fileHandle=$obj->param('fileContent');
writeLog("fileHandle:$fileHandle");
open(OUTFILE,">$AttachFile");
while ($bytesread=read($fileHandle,$buffer,1024)) {
print OUTFILE $buffer;
}
close(OUTFILE);
writeLog("Download file, checking stats.");
#
# Find out if the file was correctly uploaded. If it was not the file size will be 0.
#
($size) = (stat($AttachFile))[7];
眼下的問題是,在其名稱中沒有空間的文件這只是工作,否則$大小爲0。我在網上閱讀,似乎都的Java文件和Perl文件句柄工作與空間,所以我是什麼我做錯了?
nitpick:'OUTFILE'是你的代碼中的文件句柄。 '$ fileHandle'就是你正在處理的文件的** NAME **。它只是一個變量名稱,但至少可以使名稱反映該變種的預期用途。否則,你可能會稱它爲'$ diaper'或'$ rubber_baby_buggy_bumpers',它會爲它做的所有事情做好準備。 –
'read($ fileHandle,...)'沒有辦法工作,如果它只是一個從我認爲是CGI對象中收集的字符串。既然你說它適用於某些文件名,我假設你沒有發佈你使用的代碼。你永遠不應該這樣做。總是檢查你的代碼編譯。 – TLP