如何从多部分表单中保存多个传递给servlet的参数?

时间:2014-01-31 22:53:42

标签: java html servlets multipartform-data

我无法弄清楚如何使用getParts()来保存参数。

我正在尝试传递类型文本的4-5个输入以及图像文件。我想将参数检索到字符串中,以便将它们添加到数据库中,并将数据以blob格式保存在数据库中(而不是在服务器目录中)。

任何帮助都将受到高度赞赏。在此先感谢

<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>

2 个答案:

答案 0 :(得分:2)

您可以试试apache FileUpload

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

然后您可以像这样处理您的多部分请求

// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();

    if (item.isFormField()) {
        processFormField(item);
    } else {
        processUploadedFile(item);
    }
}

答案 1 :(得分: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!!!!!!!!!");}
       }