From: Gary C. <gc...@ir...> - 2014-12-24 09:51:03
|
Hi All, I meet a weird situation in handling attachment(Content-Disposition: attachment;filename="aa.xls"). The size of the file saved into the disk is around 1M (sometimes 1025KB, sometimes 1026KB). But its real size should be more than 1M. In my case, I run my test app and use the wireshark to capture the traffic, and find the attachment size is 1.8M. All the data has been returned by the server size, but the file saved is 1M. The temp file created by htmlunit is 1M too. Got the same results by using IE/Firefox. Is there anyone has idea about this? Test URL: http://www.szse.cn/szseWeb/FrontController.szse?ACTIONID=8&CATALOGID=1110&tab1PAGENUM=1&ENCODE=1&TABKEY=tab1 Test app: package htmlunittester; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.attachment.Attachment; import com.gargoylesoftware.htmlunit.attachment.CollectingAttachmentHandler; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author gchen */ public class AttachTester { protected WebClient m_webClient = null; private static final String URL_NON_GEB = "http://www.szse.cn/szseWeb/FrontController.szse?ACTIONID=8&CATALOGID=1110&tab1PAGENUM=1&ENCODE=1&TABKEY=tab1"; private static final String FILE_NAME_NON_GEB = "NonGEB.xls"; private static final String FILE_STORE_PATH = "c:\\"; protected final void setupWebClient() { m_webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8); //Setup this web client. m_webClient.getOptions().setJavaScriptEnabled(true);//Default is true. m_webClient.getOptions().setThrowExceptionOnScriptError(true);//Default is true. m_webClient.getOptions().setCssEnabled(false);//Default is true. m_webClient.getOptions().setThrowExceptionOnScriptError(true); //Default is true. m_webClient.setJavaScriptTimeout(0); m_webClient.getOptions().setPrintContentOnFailingStatusCode(false); } protected void cleanWebClient() { if (m_webClient != null) { m_webClient.closeAllWindows(); m_webClient.getCookieManager().clearCookies(); } } public AttachTester() { setupWebClient(); } public void test() throws IOException { String strURL = URL_NON_GEB; String strFileName = FILE_NAME_NON_GEB; final List<Attachment> attachments = new ArrayList<Attachment>(); this.m_webClient.setAttachmentHandler(new CollectingAttachmentHandler(attachments)); this.m_webClient.getOptions().setTimeout(90000 * 3); //3 times as the default time out. Page page = this.m_webClient.getPage(strURL); // List<NameValuePair> nvps = page.getWebResponse().getResponseHeaders(); // for (NameValuePair nvp : nvps) { // System.out.println(nvp.getName() + "=" + nvp.getValue()); // } if (attachments.isEmpty()) { System.out.println("Get no attachment."); return; } final Attachment result = attachments.get(0); //String filename = new String(result.getSuggestedFilename().getBytes(Charset.forName("GBK"))); //System.out.println("Suggested file name =" + filename); try { InputStream is = result.getPage().getWebResponse().getContentAsStream(); FileOutputStream out = new FileOutputStream(new File(FILE_STORE_PATH + strFileName)); int read = 0; byte[] bytes = new byte[1024]; while ((read = is.read(bytes)) != -1) { out.write(bytes, 0, read); } is.close(); out.flush(); out.close(); // System.out.println("New file created!"); } catch (IOException e) { System.out.println(e.getMessage()); } } /** * @param args the command line arguments */ public static void main(String[] args) { try { AttachTester tester = new AttachTester(); tester.test(); } catch (Exception ex) { Logger.getLogger(HtmlUnitTester.class.getName()).log(Level.SEVERE, null, ex); } } } Best Regards, Gary |