如何在我的代码中访问从URL下载的附件?

时间:2014-05-07 21:12:50

标签: java xml servlets xml-parsing httpurlconnection

当我在servlet 1中调用servlet 2的URL时

URLConnection urlConnection = someURL.openConnection();

响应是XML文件附件,Content-Disposition: attachment; filename=xxxx Content-Type:XML

现在我想解析附件XML文件,我试过BufferedReader bf = new BufferedReader(new InputStreamReaderurlConnection .getInputStream())),但是bf为null。

这是有道理的,因为如果我尝试直接在Chrome中打开链接,它会自动将此XML保存到我的本地磁盘,并且响应正文为空。在Firefox和IE中,我有机会打开它或另存为,只有在浏览器中打开时,响应才会包含XML文件。

有人能开导我一点吗?

编辑1: 我尝试使用curl但似乎它不适用于https。 这是一些代码:

URL qkrFileUrl = new URL(requestURL);

HttpURLConnection httpUrlConnection = (HttpURLConnection) qkrFileUrl
                .openConnection();
httpUrlConnection.setRequestProperty("Accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpUrlConnection.setRequestProperty("Accept-Encoding",
                "gzip,deflate");
httpUrlConnection.setRequestProperty("Accept-Language:",
                "en-us,de-de;q=0.7,en;q=0.3");
httpUrlConnection.setRequestProperty("Connection:", "keep-alive");
httpUrlConnection.setRequestProperty("Host:",
                "connectionsww.demos.ibm.com");
httpUrlConnection.setRequestProperty("User-Agent",
                    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0");

httpUrlConnection.setAllowUserInteraction(false);
BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(httpUrlConnection.getInputStream()));


        LOGGER.debug("bufferedReader Content is " + bufferedReader.toString());

        String line = null;
        String XMLFile = new String();
        while (bufferedReader.readLine() != null) {
            line = bufferedReader.readLine();
            LOGGER.debug("Line is " + line);
            XMLFile += line;
        }

        LOGGER.info("XML file from the qkr file is " + XMLFile);

,XMLFile为null。

请求属性与我在Chrome中发出请求的属性相同。如果我没有设置请求属性,我会在XMLFile中获得一些奇怪的javascript:

  

错误500:java.net.MalformedURLException:no protocol:if(self   != top){try {// per Origin Origin Policy,尝试获得最佳位置   attributes,if(self.location.hostname!= top.location.hostname){   top.location = self.location; }赶上(E){   document.getElementsByTagName('body')[0] .style.display ='none'; }}    iContainer_skip_init = true; document.getElementsByTagName(“html”)[0] .className + =“   lotusui_ie lotusui_ie6“;< / script> document.getElementsByTagName(”html“)[0] .className + =”   lotusui_ie8“;< / script>

找出答案   更多在ibm.com

  

尝试   IBM Connections产品文档

欢迎使用IBM Connections    用户名:

密码:

IBM serviceName:“webresources”,   contextRootEnabler:“/ connections / resources”,}; var baseHost =   “connectionsww.demos.ibm.com”; var needProxy =(baseHost!=   window.location.host || baseProtocol +“:”!= window.location.protocol);   isDebug:false,locale:“en-us”,blankGif:   “https://connectionsww.demos.ibm.com/connections/resources/web/com.ibm.lconn.core.styles.oneui3/images/blank.gif?etag=20140502.131757”,   proxy:“/ connections / resources / ajaxProxy”,};基础:   “https://connectionsww.demos.ibm.com/connections/resources”,参数:   “etag = 20140502.131757& lang = en& country = US”CKEDITOR_BASEPATH =   window.CKEDITOR_BASEPATH ||   “https://connectionsww.demos.ibm.com/connections/resources/web/com.ibm.oneui.ckeditor/editor/”;      dojo.addOnLoad(function(){if(fragment){fragment =   fragment.substring(1); input.value = input.value ||分段;尝试{如果   (dojo.indexOf([_ u,_ p],document.activeElement)=== -1)} catch(e){}   var redirect =   findRedirectInFragment(“/ DM /原子/库/ ECD7B321-7729-430F-9F5C-9279D83EF13E; D826C8CD-2B72-4546-82AF-A3D23377C061 /文件/%7BF2CA2079-F96F-4B3C-8B87-5CB0B616D743%7D /媒体/ install.log的.qkr”, “”, “”);   function findRedirectInFragment(redirect,base,query){if(hash){   if(hash.length> 0){hash = hash.substring(1); if(queryMarker!=   -1){hash = hash.substring(0,queryMarker); if(hash.charAt(0)==“/”&& base.charAt(base.length-1)==“/”)base + = hash; } return redirect;   空

0 个答案:

没有答案