0

我有一个调用我的 Perl 脚本来上传文件的 java 程序。它有一个 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 文件句柄都可以使用空格,所以我做错了什么?

4

1 回答 1

3

您糟糕的变量命名使您陷入困境:

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

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

您试图从不是文件句柄的东西中读取,它只是一个名称恰好是“文件句柄”的变量。您从未打开指定的文件进行阅读。例如你失踪了

open(INFILE, "<$fileHandle");

read(INFILE, $buffer, 1024);
于 2013-10-18T18:34:40.560 回答