2011-11-25 67 views
-1

可能重複:
How to upload a file using Java HttpClient library working with PHP - strange problem如何使用java提交圖像文件的表單?

我試圖創建一個腳本,可以在網站上自動提交表單具有兩個字段:標題和說明,文件輸入,我應該上傳一張圖片。 最後的日子我已經搜查每一頁我在谷歌找到,但我不能解決我的問題...... 我還需要發佈一個cookie,我做了使用cookie:

connection.setRequestProperty("Cookie", cookie); //this worked 

但我提交表單,第一I'we使用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? 非常感謝您的諮詢!

+2

所以你想使用JAVA或PHP?因爲你的HTML表單提交給PHP ... – GEMI

+0

我想用java來提交表單。 – coolboycsaba

+0

html表單不在我的網站上,我需要每天至少提交一次或兩次該表單,並且由於這個原因,我想自動填寫並提交表單給我使用的腳本 – coolboycsaba

回答

0

我最近做這個使用Spring Web MVC框架和Apache通用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中的uploadDocument屬於org.springframework.web.multipart.CommonsMultipartFile類型,可直接在控制器中訪問(multipartResolver自動分析每個多部分請求)