perl filehandle不会读取名称中包含空格的文件

时间:2013-10-18 18:29:39

标签: java perl file-upload filehandler

我有一个调用我的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文件句柄都有空间,所以我做错了什么?

1 个答案:

答案 0 :(得分:3)

你糟糕的变量命名使你绊倒了:

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

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

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

open(INFILE, "<$fileHandle");

read(INFILE, $buffer, 1024);