如何使用java提交带有图像文件的表单?

如何使用java提交带有图像文件的表单?,java,forms,httpclient,Java,Forms,Httpclient,可能重复: 我正在尝试创建一个脚本,可以在网站上自动提交一个表单,其中包含两个字段:标题和描述,以及一个文件输入,我应该在其中上传图像。 最近几天我搜索了在谷歌上找到的每一个页面,但我无法解决我的问题。。。 我还需要发布一个cookie,我使用以下方法制作了cookie: connection.setRequestProperty("Cookie", cookie); //this worked 但是我在提交表单时遇到了问题,首先我尝试使用HttpUrlConnection,但我无法理解,现

可能重复:

我正在尝试创建一个脚本,可以在网站上自动提交一个表单,其中包含两个字段:标题和描述,以及一个文件输入,我应该在其中上传图像。 最近几天我搜索了在谷歌上找到的每一个页面,但我无法解决我的问题。。。 我还需要发布一个cookie,我使用以下方法制作了cookie:

connection.setRequestProperty("Cookie", cookie); //this worked
但是我在提交表单时遇到了问题,首先我尝试使用HttpUrlConnection,但我无法理解,现在我正在尝试使用HttpClient解决我的问题
html表单如下所示:

<form action="submit.php" method="post" enctype="multipart/form-data">
<input type="text" name="title">
<input name="biguploadimage" type="file">
<textarea name="description"></textarea>
<input type="image" src="/images/submit-button.png">
</form>

我的图像位于
d:/images/x.gif
请提供完整的代码,因为我是java新手。 O、 以及如何使用HttpClient创建cookie?
非常感谢你的建议

有一篇很好的文章提供了代码示例:

注意MultipartPostMethod。这将允许您在一个请求中发布文件和其他数据。
如何使用此处描述的许多参数进行简单的发布:

此url可能会帮助您解决问题。这不是那么简单,否则我会把代码粘贴到这里

你也可以看看这个问题

我最近使用Spring Web MVC和Apache Commons FileUpload实现了这一点:

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

(...)

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView uploadFile(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView modelAndView = new ModelAndView("view");

        if (ServletFileUpload.isMultipartContent(request)) {
            handleMultiPartContent(request);
        }

        return modelAndView;
    }


    private void handleMultiPartContent(HttpServletRequest request) {

        ServletFileUpload upload = new ServletFileUpload();
        upload.setFileSizeMax(2097152); // 2 Mb
        try {
            FileItemIterator iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                if (!item.isFormField()) {
                    File tempFile = saveFile(item);
                    // process the file
                }
            }
        }
        catch (FileUploadException e) {
            LOG.debug("Error uploading file", e);
        }
        catch (IOException e) {
            LOG.debug("Error uploading file", e);
        }
    }

    private File saveFile(FileItemStream item) {

        InputStream in = null;
        OutputStream out = null;
        try {
            in = item.openStream();
            File tmpFile = File.createTempFile("tmp_upload", null);
            tmpFile.deleteOnExit();
            out = new FileOutputStream(tmpFile);
            long bytes = 0;
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
                bytes += len;
            }
            LOG.debug(String.format("Saved %s bytes to %s ", bytes, tmpFile.getCanonicalPath()));
            return tmpFile;
        }
        catch (IOException e) {

            LOG.debug("Could not save file", e);
            Throwable cause = e.getCause();
            if (cause instanceof FileSizeLimitExceededException) {
                LOG.debug("File too large", e);
            }
            else {
                LOG.debug("Technical error", e);
            }
            return null;
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
            catch (IOException e) {
                LOG.debug("Could not close stream", e);
            }
        }
    }
这会将上载的文件保存到临时文件中

如果您不需要对上传进行所有低级控制,那么使用CommonsMultipartResolver会简单得多:

<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="2097152"/>
</bean>

jsp中的一个示例表单:

<form:form modelAttribute="myForm" method="post" enctype="multipart/form-data">
    <form:input path="bean.uploadedFile" type="file"/>
</form>


bean中上载的文档类型为org.springframework.web.multipart.commonmultipartfile,可以在控制器中直接访问(multipartResolver自动解析每个multipart请求)

那么您想使用JAVA还是PHP?因为你的HTML表单提交给PHP…我想用java来提交表单。HTML表单不在我的网站上,我需要每天至少向该表单提交一到两次,因此我想制作一个脚本,自动填充并提交表单(实际上,只需在Google上搜索“httpclient上载文件”)。