James
James

Reputation: 31

Creating directories problem

Can't figure out why this keeps creating 2 folders? It makes a '0' folder and whatever the jobID is from the html. I want uploaded files in the jobID folder, not the '0' folder.

        int userID = 1; // test
        String coverLetter = "";
        String status = "Review";
        int jobID = 0;
        String directoryName = "";

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if(isMultipart && request.getContentType() != null)
        {
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            // Parse the request
            List /* FileItem */ items = null;
            try
            {
                items = upload.parseRequest(request);
            }
            catch(FileUploadException e) {}

            // Process the uploaded items
            Iterator iter = items.iterator();

            while(iter.hasNext())
            {
                FileItem item = (FileItem)iter.next();

                if(item.isFormField())
                {
                    if(item.getFieldName().equals("coverLetter"))
                        coverLetter = item.getString();
                    if(item.getFieldName().equals("jobID"))
                        jobID = Integer.parseInt(item.getString()); 
                }

                directoryName = request.getRealPath("/") + "/Uploads/CV/" + jobID + "/";    

                File theDir = new File(directoryName);
                if (!theDir.exists())
                    theDir.mkdir();

                if(item.getFieldName().equals("file"))
                {   
                    File uploadedFile = new File(directoryName + item.getName());
                    try
                    {
                        item.write(uploadedFile);
                    }
                    catch(Exception e) {}
                }
}

Edit:

Problem solved.I want uploaded files

It was because it was in the jobID folder, not the '0' folder.

Upvotes: 1

Views: 101

Answers (3)

rogermushroom
rogermushroom

Reputation: 5586

There must be 2 items parsed from the request, So perhaps you are sending 2 upload items.

The first item doesn't have the jobID FieldName so the directory name remain

.../Uploads/CV/0

So thats the time which is causing problems.

The second item does have the job ID so the directory gets created correctly.

Can you post the form so we can see, it may be something on there. Is the cover letter an additional file without jobId?

You could solve it by only creating dir if jobID exists.

Upvotes: 1

adarshr
adarshr

Reputation: 62583

Try printing/logging the jobID before the below line:

directoryName = request.getRealPath("/") + "/Uploads/CV/" + jobID + "/";

Upvotes: 0

rich
rich

Reputation: 19404

I suspect this isn't true:

item.getFieldName().equals("jobID")

It's a bit difficult to guess though. Have you tried debugging in Eclipse (or similar)? Adding some logging might help too.

Upvotes: 1

Related Questions