gpt4 book ai didi

java - perl 文件句柄不读取名称中带有空格的文件

转载 作者:行者123 更新时间:2023-12-01 23:30:15 24 4
gpt4 key购买 nike

我有一个 java 程序,它调用我的 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];

现在的问题是这只适用于名称中没有空格的文件,否则 $size 为 0。我在网上阅读,似乎 Java 文件和 Perl 文件句柄都可以使用空格,那么我做错了什么?

最佳答案

你糟糕的变量命名让你陷入了困境:

open(OUTFILE,">$AttachFile");
^^^^^^^---this is your filehandle

while ($bytesread=read($fileHandle,$buffer,1024)) {
^^^^^^^^^^^--- this is just a string

您正在尝试读取不是文件句柄的内容,它只是一个名称恰好为“filehandle”的变量。您从未打开指定的文件进行读取。例如你失踪了

open(INFILE, "<$fileHandle");

read(INFILE, $buffer, 1024);

关于java - perl 文件句柄不读取名称中带有空格的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19456659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com