简体   繁体   English

perl filehandle不会读取名称中带有空格的文件

[英]perl filehandle doesn't read file with space in its name

I have a java program that call my Perl script to upload a file. 我有一个Java程序,该程序调用Perl脚本来上传文件。 It has a file parameter to the Perl script that contain the location of file to upload. 它对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();
    }
}

In this part of my Perl script, it get the information about the file, read from it and write the content to a blink file in my server. 在我的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];

Right now the problem is this only work for file with no space in its name, otherwise $size is 0. I was reading online and it seems both Java file and Perl filehandle work with space, so what am I doing wrong? 现在的问题是,这仅适用于名称中没有空格的文件,否则$ size为0。我正在在线阅读,看来Java文件和Perl filehandle都可以使用空格,那么我在做什么错呢?

Your poor variable naming has tripped you up: 可怜的变量命名使您大跌眼镜:

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

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

You're trying to read from something that's NOT a filehandle, it's just a variable whose name happens to be "filehandle". 您正在尝试从不是文件句柄的内容中读取内容,它只是一个变量,其名称恰好是“文件句柄”。 You never opened up the specified file for reading. 您从未打开指定的文件进行读取。 eg you're missing 例如你失踪了

open(INFILE, "<$fileHandle");

read(INFILE, $buffer, 1024);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM