-1

I've recently just got my web application on a linux server running tomcat. Running the application locally on my Windows computer allows me to upload an XML file with no problem, but now that it's on the tomcat server, I keep getting NullPointerExceptions.

Here is the part of the servlet that receives the file:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ParserConfigurationException, SAXException, ResponseException {
    response.setContentType("text/html;charset=UTF-8");

    //Create path components to save the file
    final Part filePart = request.getPart("XMLFile"); // Retrieves <input type="file" name="XMLFile">
    final String path = "/opt/tomcat/bin";
    final String fileName = getFileName(filePart);

    OutputStream outStream = null;
    InputStream fileContent = null;
    final PrintWriter out = response.getWriter();

    try {
        //File xmlFile = new File(path + File.separator + fileName);
        File xmlFile = new File(fileName);
        outStream = new FileOutputStream(xmlFile);
        fileContent = filePart.getInputStream();
...

The line "File xmlFile = new File(fileName)" throws a FileNotFoundException in a later catch statement. I've tried a bunch of different possibilities, but I can't find the error.

With some lines of code I end up with the file path being "opt/tomcat/bin/(name of my xml file)" and other times it will be "opt/tomcat/bin/opt/tomcat/bin/(name of my xml file)" but none of them work.

Any ideas what the error could be?

Here's the form on my JSP page that handles the file upload:

<form style="text-align: center" style="position: relative" style="right: auto" style="left: auto" action="upload" method="post" enctype="multipart/form-data">

            <h3>Upload your iTunes library</h3>
            <input style="margin: 0 auto" name="XMLFile" id="file" type="file"/>
            <br>
            <input type="submit" value="Submit" formmethod="post" formaction="GetItunesLibrary"/>
</form> 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tim
  • 315
  • 4
  • 23

1 Answers1

1

First of all, you shouldnt store your files in /opt/tomcat/bin. Use another folder, which doesn't need root access. Something like /home/<user>/uploads or similar. If your tomcat appliance on linux is running under the same user, which you specify above, you won't need to set extra permissions..

If you specify a File instance, to write the contents into.. use the full path like:

/home/<user>/uploads/<filenameToStore>

In your case, you have created a File instance to a probably non existing physical file.

So according to your code, use

File outputFile = new File(path + File.separator + fileName);

if (!outputFile.exists())
     outputFile.createNewFile();

where path is something like /home/<user>/uploads. Note this assumes, that the folder uploads is existing.

On linux you should always make sure, that you or better said tomcat have the desired permissions to read/write/create data in your specified folders

lunatikz
  • 716
  • 1
  • 11
  • 27
  • Hello! I took your advice and changed the path to /home/uploads. However, the error still remains. It keeps saying that the file could not be found in /opt/tomcat/bin/opt/tomcat/bin/. I've looked over this code what feels like a thousand times now and I have no idea what I'm doing wrong – Tim Jan 30 '16 at 20:15
  • Here's the full script: http://pastebin.com/kv9TpvkX – Tim Jan 30 '16 at 20:16
  • 1
    Try this: String filePath = request.getRealPath("/home/uploads");// note that this requires that your folder structure is YOUR_HOME/home/uploads and then call a two-args File constructor thus File xmlFile = new File(filePath, fileName); I don't know why , but calling a one-arg construcor didn't work for me (I was getting FileNotFound like you) – dsp_user Jan 31 '16 at 13:12
  • @Tim: its strange, that exception points out, that the file couldnt be found in your mentioned location. I'd suggest, you carefully debug your values, if you hadnt done it before. Also check if your `File` instance has the correct value, by using `getAbsolutePath()`. If thats also not working, my next step would be to create a new simple example uploading servlet and test with that one. In my case the above mentioned code is working fine. Also see the linked answers by BalusC, they'll provide some very valueable insights serlvets and uploading – lunatikz Jan 31 '16 at 14:05
  • Have you also made sure, that your mentioned servlets gets actually called ? In your path specified path `/home/uploads/` the user is also missing, which could lead to permission problems. Also you should make sure, that you close the used streams. – lunatikz Jan 31 '16 at 14:20
  • @lunatikz Is the user part a necessity? My website doesn't have users. Basically you just upload the file and then the server parses through the file and tells you some stuff about it. Creating users seemed unnecessary – Tim Jan 31 '16 at 17:12
  • 1
    I think you mix up, users of your website with the user environment within your linux server.. I'm talking about the linux users only. If you start your tomcat, you start it within a specific user context of linux. (e.g. the user, which you use to connect to your linux server via ssh). A necessity it is not, but in this case, you have to carefully set the file permissions for the folders. – lunatikz Jan 31 '16 at 17:28
  • Ah ok, thanks. I figured out why my code wasn't working. In the constructor for my ScanItunesSongs class, I gave it a fileLocation as a parameter (which I gave "home/uploads") but when I called the scan() function, I was changing it back to "opt/tomcat/bin." I feel like an idiot, haha. Thanks for the help – Tim Jan 31 '16 at 17:33
  • 1
    Dont mind.. welcome :) Glad that your issue is fixed. – lunatikz Jan 31 '16 at 17:35