google-app-enginegwtinputstreamgwtuploadfileitem

GWTUpload InputStream returning -1


I am having a problem with parsing a CSV file after uploading it to GAE using GWTUpload. The upload seems to work fine but I am having a problem reading data from the file using the getInputStream method from FileItem. My code looks like this:

for (FileItem item : sessionFiles) {
      if (!item.isFormField()) {
          logger.log(Level.INFO, "file item is: " + item.getName());
          try{
              /* The below line prints -1 when uncommented
               * logger.log(Level.INFO, "input stream read is: " + item.getInputStream().read());
               */
              InputStreamReader insr = new InputStreamReader(item.getInputStream());

              //This is just for debugging purposes
              //String insString = Integer.toString(insr.read());

              BufferedReader br = new BufferedReader(insr);
              //logger.log(Level.INFO, "first row is: " + br.readLine());
              logger.log(Level.INFO, "calling on parseCSV");
              parseCSV(br);
          }
          catch(Exception e){
              throw new UploadActionException(e);
          }
      }
    }

I am only uploading a single file using SingleUploader and it seems to be uploaded correctly. For example, the

logger.log(Level.INFO, "file item is: " + item.getName());

line outputs CSVExample.csv correctly. However, the

logger.log(Level.INFO, "file item is: " + item.getName());

line always outputs -1, indicating the end of the stream.

My csv file looks like this:

,vendor_food,open,emptyKey,,Hot Dogs,49.28241993,-123.1243637
DT56,vendor_food,open,invalidLat,Coal Harbour Seawall  - 200 Metres Northeast of Broughton St,Hot Dogs,tt,-123.1236823
E423,vendor_food,open,invalidLon,,Hot Dogs,49.28449252,tt
EB05,vendor_food,open,emptyLat,,Hot Dogs,,-123.1432294
EB07,vendor_food,open,emptyType,,,49.28763934,-123.1425256
GM02,vendor_food,pending,closed,,Hot Dogs,49.28298256,-123.1172526
GM05,vendor_food,open,valid,sadsadsadsadsadsad,Hot Dogs,49.28051786,-123.1209519

Does anyone have any suggestions as to what I am doing wrong?


Solution

  • This was a while ago but I thought I'd update in case someone else comes across this one day.

    From the time the file was uploaded, to the time that I went to parse it in the code block above, the input stream from each FileItem had already been accessed. So when I went to read the input stream there was nothing there. I can't remember exactly where it was being accessed but I vaguely remember it happening in the doGet method.