Java获取URL链接的文件类型

在写网络爬虫的时候,需要根据链接来获取文件类型,将内容正确存储。之前我都是根据链接的后缀来判断的,比如:

http://img12.360buyimg.com/da/20120330/88_31_ZySDre.jpg

这个链接指向的文件就是个jpg文件。但是后来发现有诸如

http://jprice.360buyimg.com/getSkuPriceImgService.action?skuId=1850001109&origin=1&webSite=1&type=1的链接,这招就不灵了。后来谷歌百度了一下也没发现解决办法。后来机缘巧合在Java Network Programming上找到了一个办法:

URLConnection class provides two static methods to help programs figure out the MIME type of some data; you can use these if the content type just isn't available or if you have reason to believe that the content type you're given isn't correct。

就是说URLConnection提供了两种方法可以猜测(根据实测结果,这个猜测是相当的准)数据的MIME类型。

  第一个是:

public static String guessContentTypeFromName(String name)

这个方法根据URL文件部分的后缀名来判断类型,跟之前我的方法一样。这个不能解决上面那个问题。
第二个是:
public static String guessContentTypeFromStream(InputStream in)
这个方法是根据流的前面几个字节来判断类型,这个就不需要文件后缀名了,完全可以解决上面那个问题。

测试代码如下:
?
BufferedInputStream bis = null ;
HttpURLConnection urlconnection = null ;
URL url = null ;        
         url = new  URL(strUrl);
     urlconnection = (HttpURLConnection) url.openConnection();
     urlconnection.connect();
bis = new  BufferedInputStream(urlconnection.getInputStream());
     System.out.println( "file type:" +HttpURLConnection.guessContentTypeFromStream(bis));

以上


image
 
  
java.net.ProtocolException: Can't reset method: already connected
	at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:314)
	at MultiDowloader.run(MultiDowloader.java:33)

今天写多线程下载的时候总是遇到以上的问题

import java.net.*;
import java.io.*;
public class BadURLPost
{
    public static void main(String args[])
    {
        // get an HTTP connection to POST to
       
        try
        {
            // get the url as a string
            String surl ="http://www.foshanshop.net/ejb3/ActivePort.exe";
            URL url = new URL(surl);
            URLConnection con = url.openConnection();
            System.out.println("Received a : " + con.getClass().getName());
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            String msg = "Hi HTTP SERVER! Just a quick hello!";
            con.setRequestProperty("CONTENT_LENGTH", "5"); // Not checked
            con.setRequestProperty("Stupid", "Nonsense");
            System.out.println("Getting an output stream...");
            OutputStream os = con.getOutputStream();
            System.out.println("Getting an input stream...");
            InputStream is = con.getInputStream();
            /*
            con.setRequestProperty("CONTENT_LENGTH", "" + msg.length());
            Illegal access error - can't reset method.
            */
            OutputStreamWriter osw = new OutputStreamWriter(os);
            osw.write(msg);
            osw.flush();
            osw.close();
            System.out.println("After flushing output stream. ");
            // any response?
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ( (line = br.readLine()) != null)
            {
                System.out.println("line: " + line);
            }
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
} 


原文转自:http://www.cnblogs.com/syncg/archive/2011/04/07/2008242.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值