1

I couldn't figure out how to use getParts() to save the parameters.

I am trying to pass 4-5 inputs of type text along with an image file. I want to retrieve the parameters into strings so I can add them to the database and save the image in a blob format in the database (not in the server directory).

Any Help would be Highly appreciated. Thanks in Advance

<form class="form-signin" action="createAlbum" method="post" enctype="multipart/form-data" role="form"> <h2 class="form-signin-heading"> Add details to create an Album. </h2> <input type="text" name="aname" class="form-control" placeholder="Album Name" required> <input type="text" name="artists" class="form-control" placeholder="Artist" required> <input type="date" name="rdate" class="form-control" placeholder="Release Date" required > <input type="text" name="type" class="form-control" placeholder="Genre" required > <input type="text" name="price" class="form-control" placeholder="Price"> <input type="file" name="picture" class="form-control" placeholder="Picture" required > <button class="btn btn-lg btn-primary btn-block" type="submit">Add Album</button> </form> 
3
  • You should post the section of your Servlet code where you are trying to retrieve the parameters. There is no real way to help you without seeing some code. It will also help if you give more detail about what you have tried and the results; are there any errors or exceptions? Are some of the parameters received but not others? If you provide more details, you will give those that want to help a better idea of your problem. Commented Feb 1, 2014 at 0:39
  • not sure about the solution you're using, but you may find this somewhat useful commons.apache.org/proper/commons-fileupload/using.html Commented Feb 1, 2014 at 0:45
  • 1
    I was using getParts(). I had no idea what to do with it. Thanks to @Leo i used their library support and completed it. Commented Feb 1, 2014 at 4:00

2 Answers 2

2

You may give apache FileUpload a try

http://commons.apache.org/proper/commons-fileupload/using.html

then you could process your multipart request like this

// Process the uploaded items Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); if (item.isFormField()) { processFormField(item); } else { processUploadedFile(item); } } 
Sign up to request clarification or add additional context in comments.

Comments

1
PrintWriter out = response.getWriter(); System.out.println("Request content length is " + request.getContentLength() + "<br/>"); System.out.println("Request content type is " + request.getHeader("Content-Type") + "<br/>"); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(isMultipart){ ServletFileUpload upload = new ServletFileUpload(); try{ FileItemIterator iter = upload.getItemIterator(request); FileItemStream item = null; String name = ""; InputStream stream = null; while (iter.hasNext()){ item = iter.next(); name = item.getFieldName(); stream = item.openStream(); if(item.isFormField()){System.out.println("Form field " + name + ": " + Streams.asString(stream) + "<br/>");} else { name = item.getName(); System.out.println("name==" + name); if(name != null && !"".equals(name)){ String fileName = new File(item.getName()).getName(); out.write("Client file: " + item.getName() + " <br/>with file name " + fileName + " was uploaded.<br/>"); File file = new File(getServletContext().getRealPath("/WEB-INF/temp/" + fileName)); FileOutputStream fos = new FileOutputStream(file); long fileSize = Streams.copy(stream, fos, true); System.out.println("Size was " + fileSize + " bytes <br/>"); System.out.println("File Path is " + file.getPath() + "<br/>"); } } } } catch(FileUploadException fue) {out.write("fue!!!!!!!!!");} } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.