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

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

最佳答案

你可以试试 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);
    }
}

关于java - 如何保存从多部分表单传递给 servlet 的多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21491551/

相关文章:

java - 当我尝试使用 Android 中的 native 电子邮件客户端发送电子邮件时,href 不起作用

Java:JScrollPane 不适用于 GridBagLayout

javascript - 如何访问div内的视频元素

javascript - 显示 chop 字符串的显示值

java - 无法从 Bootstrap 日期选择器获取日期

java - 根上下文和调度程序 servlet 上下文到底是如何进入 Spring MVC Web 应用程序的?

java - Grails:在 BootStrap.groovy 中获取上下文路径

java - useSystemProperties() 不适用于 Apache HttpClientBuilder.create() 版本 4.5.6

java - JComboBox 和文件读取

页面加载时的 JavaScript 加载屏幕