0

I have a java program that call my Perl script to upload a file. It has a file parameter to the Perl script that contain the location of file to upload.

    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.

#
# 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?

help
  • 809
  • 5
  • 18
  • 35
  • 1
    nitpick: `OUTFILE` is a file handle in your code. `$fileHandle` is simply the **NAME** of a file that you're dealing with. It's just a variable name, but at least make the name reflect what the var's intended purpose is. Otherwise you might as well call it `$diaper` or `$rubber_baby_buggy_bumpers` for all the good it'll do. – Marc B Oct 18 '13 at 18:32
  • 1
    There is no way that `read($fileHandle, ...)` works, if it is only a string collected from what I assume is a CGI object. Since you say it works for some file names, I assume that for some reason you've not posted the code you used. You should never, ever do that. Always check that your code compiles. – TLP Oct 18 '13 at 18:42

1 Answers1

3

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. e.g. you're missing

open(INFILE, "<$fileHandle");

read(INFILE, $buffer, 1024);
Marc B
  • 356,200
  • 43
  • 426
  • 500