4 Replies Latest reply: Dec 28, 2009 4:02 PM by 807580 RSS

    Uploading files using POST

      Hi everyone!
      I'm developing a function in which I have to upload a local file into a server and that server will introduce it's info in the database. I have to do this via POST. When I execute it, the console tells me that the POST was done OK but then when I go and see the server the database has not been modified. Any ideas of why it isn't working? You can see the code which I'm using below. Thanks for your help!!
                  File f = new File("userdownload.csv");
                  PostMethod post = new PostMethod(prop.getProperty("SERVER" + pair + ".Host") + path);
                  Part[] parts = {new FilePart("usersCsvFile", f),new StringPart("process","uploadEvent")};
                  post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
                  int status = client.executeMethod(post);
        • 1. Re: Uploading files using POST
          You'll have to debug what's happening on the server side to determine:
          1) If it is actually being invoked
          2) What the data is that it is receiving from the request
          3) What it is doing with the data
          • 2. Re: Uploading files using POST
            Thanks for answering so fast. I can't debug on the server side but I can show what's happening on the logs of the server. As you can see below, login succeeds but when I try to upload it gives an error...

            INFO,2009/12/28 16:08:06Login,Login succeeded.,admin
            WARN,2009/12/28 16:08:28,The program is abnormal,Failed to retrieve the User CSV file.,admin

            Edited by: javajuniordeveloper on Dec 28, 2009 7:31 AM
            • 3. Re: Uploading files using POST
              You can't just continue treating the server code being executed like it's a 'black box'. The server side code obviously failed and logged that (not very descriptive nor diagnosable) error message, so you know it is failing. The next step is to add better diagnostics on the server side so you can see better what you are doing wrong in the request.
              Maybe your parameter name "usersCsvFile" is misspelled (so maybe the server is looking for a parameter named something else)? Maybe the path to your csv file is wrong (so no file is actually being uploaded)? The code being executed on the server side knows what the problem is, but currently it is not logging enough of the problem to communicate that to you. You need to get someone on the server side of things to make it better at error handling so it can tell you what the problem is rather than summarizing it as simply "Failed to retrieve the User CSV file"
              • 4. Re: Uploading files using POST
                I've found the problem, I've found another log on the server machine which gave an error of duplicated key entry. That's why it was not inserting anything on the database.
                Thanks for your help anyway.