下面列出了java.net.URLConnection#getInputStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* tries to get the newest MineTinker-Version number from api.spigotmc.org
*/
public static void checkOnline() {
if (hasUpdate()) {
return;
}
try {
URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + 58940);
URLConnection connection = url.openConnection();
Scanner scan = new Scanner(connection.getInputStream());
if (scan.hasNextLine()) {
onlineVersion = scan.nextLine();
}
scan.close();
} catch (Exception ignored) {
}
}
/**
* 下载文件到本地
*
* @param urlString
* 被下载的文件地址
* @param filename
* 本地文件名
* @throws Exception
* 各种异常
*/
public static void download(String urlString, String filename)
throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
OutputStream os = new FileOutputStream(filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
}
public static String getUrlPageHtml(String urlStr, InetSocketAddress addr) {
StringBuilder buffer = new StringBuilder();
try {
URL url = new URL(urlStr);
URLConnection connection = url.openConnection(new Proxy(Proxy.Type.HTTP, addr));
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;
while ((line = rd.readLine()) != null) {
buffer.append(line);
}
rd.close();
return buffer.toString();
} catch (Exception ex) {
XViewerLog.log(Activator.class, Level.SEVERE, "Can't getUrlPageHtml");
return simplePage("Exception opening url " + ex.getLocalizedMessage());
}
}
public void downloadConfig(String language, File file) {
getLogger().info("https://iridiumllc.com/Languages/" + language + "/" + file.getName());
try {
URLConnection connection = new URL("https://iridiumllc.com/Languages/" + language + "/" + file.getName()).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.setAllowUserInteraction(false);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
if (!file.exists()) file.createNewFile();
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
}
in.close();
out.close();
} catch (IOException e) {
IridiumSkyblock.getInstance().getLogger().info("Failed to connect to Translation servers");
}
}
/**
* Returns AudioFileFormat from URL.
*/
@Override
public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException
{
if (TDebug.TraceAudioFileReader) {TDebug.out("MpegAudioFileReader.getAudioFileFormat(URL): begin"); }
long lFileLengthInBytes = AudioSystem.NOT_SPECIFIED;
URLConnection conn = url.openConnection();
// Tell shoucast server (if any) that SPI support shoutcast stream.
conn.setRequestProperty ("Icy-Metadata", "1");
InputStream inputStream = conn.getInputStream();
AudioFileFormat audioFileFormat = null;
try
{
audioFileFormat = getAudioFileFormat(inputStream, lFileLengthInBytes);
}
finally
{
inputStream.close();
}
if (TDebug.TraceAudioFileReader) {TDebug.out("MpegAudioFileReader.getAudioFileFormat(URL): end"); }
return audioFileFormat;
}
@Override
public InputStream getImage(String path) {
try{
URL url=new URL(path);
URLConnection connection=url.openConnection();
connection.connect();
InputStream inputStream=connection.getInputStream();
return inputStream;
}catch(Exception ex){
throw new ReportException(ex);
}
}
public void testNonHexadecimalChunkSize() throws Exception {
server.enqueue(new MockResponse()
.setBody("G\r\nxxxxxxxxxxxxxxxx\r\n0\r\n\r\n")
.clearHeaders()
.addHeader("Transfer-encoding: chunked"));
server.play();
URLConnection connection = server.getUrl("/").openConnection();
InputStream in = connection.getInputStream();
try {
in.read();
fail();
} catch (IOException expected) {
}
}
public void sendRequest() throws Exception {
URLConnection connection = new URL("http://localhost:8181/cxf/HelloWorld")
.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
// Post the request file.
InputStream fis = getClass().getClassLoader().getResourceAsStream("org/apache/servicemix/samples/cxf_osgi/request.xml");
IOUtils.copy(fis, os);
// Read the response.
InputStream is = connection.getInputStream();
System.out.println("the response is ====> ");
System.out.println(IOUtils.toString(is));
}
/**
* Create a binary input stream from a URL.
*/
public BinaryIn(URL url) {
try {
URLConnection site = url.openConnection();
InputStream is = site.getInputStream();
in = new BufferedInputStream(is);
fillBuffer();
}
catch (IOException ioe) {
System.err.println("Could not open " + url);
}
}
private String getContent( String url )
throws Exception
{
String content;
URLConnection connection;
connection = new URL( url ).openConnection();
connection.addRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
Scanner scanner = new Scanner( connection.getInputStream() );
scanner.useDelimiter( "\\Z" );
content = scanner.next();
return content;
}
@Test
public void testGetBook123() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/test/bookstore/books/123";
URL url = new URL(endpointAddress);
URLConnection connect = url.openConnection();
connect.addRequestProperty("Accept", "application/json");
InputStream in = connect.getInputStream();
InputStream expected = getClass()
.getResourceAsStream("resources/expected_get_book123json.txt");
assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in));
}
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
String path = request.getPathInfo();
if (StringUtils.isEmpty(path)
|| !path.startsWith(VAADIN_WEBJARS_PATH_PREFIX)) {
return false;
}
log.trace("WebJar resource requested: {}", path.replace(VAADIN_WEBJARS_PATH_PREFIX, ""));
String errorMessage = checkResourcePath(path);
if (StringUtils.isNotEmpty(errorMessage)) {
log.warn(errorMessage);
response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMessage);
return false;
}
URL resourceUrl = getStaticResourceUrl(path);
if (resourceUrl == null) {
resourceUrl = getClassPathResourceUrl(path);
}
if (resourceUrl == null) {
String msg = String.format("Requested WebJar resource is not found: %s", path);
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
log.warn(msg);
return false;
}
String resourceName = getResourceName(path);
String mimeType = servletContext.getMimeType(resourceName);
response.setContentType(mimeType != null ? mimeType : FileTypesHelper.DEFAULT_MIME_TYPE);
long resourceCacheTime = getCacheTime();
String cacheControl = resourceCacheTime > 0
? "max-age=" + String.valueOf(resourceCacheTime)
: "public, max-age=0, no-cache, no-store, must-revalidate";
response.setHeader("Cache-Control", cacheControl);
long expires = resourceCacheTime > 0
? System.currentTimeMillis() + (resourceCacheTime * 1000)
: 0;
response.setDateHeader("Expires", expires);
InputStream inputStream = null;
try {
URLConnection connection = resourceUrl.openConnection();
long lastModifiedTime = connection.getLastModified();
// Remove milliseconds to avoid comparison problems (milliseconds
// are not returned by the browser in the "If-Modified-Since"
// header).
lastModifiedTime = lastModifiedTime - lastModifiedTime % 1000;
response.setDateHeader("Last-Modified", lastModifiedTime);
if (browserHasNewestVersion(request, lastModifiedTime)) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return true;
}
inputStream = connection.getInputStream();
copy(inputStream, response.getOutputStream());
return true;
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
/** Entity resolver that knows about AU DTDs, so no network is needed.
* @author Jesse Glick
*/
public static EntityResolver createAUResolver() {
return new EntityResolver() {
@Override
public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {
if ("-//NetBeans//DTD Autoupdate Catalog 1.0//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-1_0.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Module Info 1.0//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-info-1_0.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.0//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_0.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Module Info 2.0//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-info-2_0.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.2//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_2.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Module Info 2.2//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-info-2_2.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.3//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_3.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Module Info 2.3//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-info-2_3.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.4//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_4.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Module Info 2.4//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-info-2_4.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.5//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_5.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Module Info 2.5//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-info-2_5.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.6//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_6.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.7//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_7.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Module Info 2.7//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-info-2_7.dtd").toString()); // NOI18N
} else if ("-//NetBeans//DTD Autoupdate Catalog 2.8//EN".equals(publicID)) { // NOI18N
return new InputSource(XMLUtil.class.getResource("resources/autoupdate-catalog-2_8.dtd").toString()); // NOI18N
} else {
if (systemID.endsWith(".dtd")) { // NOI18N
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
URL u = new URL(systemID);
URLConnection oc = u.openConnection();
oc.setConnectTimeout(5000);
return new InputSource(oc.getInputStream());
}
}
};
}
/**
* Download bitmap to outputStream by uri.
*
* @param uri file path, assets path(assets/xxx) or http url.
* @param outputStream
* @param task
* @return The expiry time stamp or -1 if failed to download.
*/
@Override
public long downloadToStream(String uri, OutputStream outputStream, final BitmapUtils.BitmapLoadTask<?> task) {
if (task == null || task.isCancelled() || task.getTargetContainer() == null) return -1;
URLConnection urlConnection = null;
BufferedInputStream bis = null;
OtherUtils.trustAllHttpsURLConnection();
long result = -1;
long fileLen = 0;
long currCount = 0;
try {
if (uri.startsWith("/")) {
FileInputStream fileInputStream = new FileInputStream(uri);
fileLen = fileInputStream.available();
bis = new BufferedInputStream(fileInputStream);
result = System.currentTimeMillis() + this.getDefaultExpiry();
} else if (uri.startsWith("assets/")) {
InputStream inputStream = this.getContext().getAssets().open(uri.substring(7, uri.length()));
fileLen = inputStream.available();
bis = new BufferedInputStream(inputStream);
result = Long.MAX_VALUE;
} else {
final URL url = new URL(uri);
urlConnection = url.openConnection();
urlConnection.setConnectTimeout(this.getDefaultConnectTimeout());
urlConnection.setReadTimeout(this.getDefaultReadTimeout());
bis = new BufferedInputStream(urlConnection.getInputStream());
result = urlConnection.getExpiration();
result = result < System.currentTimeMillis() ? System.currentTimeMillis() + this.getDefaultExpiry() : result;
fileLen = urlConnection.getContentLength();
}
if (task.isCancelled() || task.getTargetContainer() == null) return -1;
byte[] buffer = new byte[4096];
int len = 0;
BufferedOutputStream out = new BufferedOutputStream(outputStream);
while ((len = bis.read(buffer)) != -1) {
out.write(buffer, 0, len);
currCount += len;
if (task.isCancelled() || task.getTargetContainer() == null) return -1;
task.updateProgress(fileLen, currCount);
}
out.flush();
} catch (Throwable e) {
result = -1;
LogUtils.e(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(bis);
}
return result;
}
public static CellInfo findMatchingCell (String mcc, String mnc, String lac, String cellId) throws IOException
{
CellInfo result = null;
StringBuffer query = new StringBuffer();
query.append(QUERY_BASE);
query.append("mcc=").append(mcc).append('&');
query.append("mnc=").append(mnc).append('&');
query.append("lac=").append(lac).append('&');
query.append("cellid=").append(cellId).append('&');
query.append(QUERY_END);
URL url = new URL(query.toString());
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
//skip header line
//id mcc mnc lac cellid lat lon created_at measured_at signal rating speed direction
line = reader.readLine();
// 528ffc2bec5ea6166f324ec8 310 4384 65 2578 41.88437 87.67694 1309934773000 1309934773000 0 0 0 0
while ((line = reader.readLine())!=null)
{
result = new CellInfo();
StringTokenizer st = new StringTokenizer(line," ");
st.nextToken();//id
st.nextToken();//mcc
st.nextToken();//mnc
st.nextToken();//cellid
st.nextToken();//lat
st.nextToken();//lon
st.nextToken();//created_at
st.nextToken();//measured_at
st.nextToken();//signal
st.nextToken();//rating
st.nextToken();//speed
st.nextToken();//direction
}
reader.close();
return result;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String rawResourcePath = request.getPathInfo();
log.debug("Attempting to GET resource: {}", rawResourcePath);
URL[] resources = getRequestResourceUrls(request);
if (resources == null || resources.length == 0) {
log.debug("Resource not found: {}", rawResourcePath);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
prepareResponse(response, resources, rawResourcePath);
OutputStream out = selectOutputStream(request, response);
try {
for (URL resource : resources) {
URLConnection resourceConn = resource.openConnection();
InputStream in = resourceConn.getInputStream();
try {
byte[] buffer = new byte[1024];
while (in.available() > 0) {
int len = in.read(buffer);
out.write(buffer, 0, len);
}
} finally {
in.close();
try {
resourceConn.getOutputStream().close();
} catch (IOException e) {
/*ignore, just trying to free resources*/
}
}
}
} finally {
out.close();
}
}
/**
* Get a Reader so that the Runtime can build a
* template with it.
*
* @param name name of template to fetch bytestream of
* @param encoding asked encoding
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found
* in the file template path.
* @since 2.0
*/
public synchronized Reader getResourceReader(String name, String encoding)
throws ResourceNotFoundException
{
if (StringUtils.isEmpty(name))
{
throw new ResourceNotFoundException("URLResourceLoader: No template name provided");
}
Reader reader = null;
Exception exception = null;
for (String root : roots)
{
InputStream rawStream = null;
try
{
URL u = new URL(root + name);
URLConnection conn = u.openConnection();
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
rawStream = conn.getInputStream();
reader = buildReader(rawStream, encoding);
if (reader != null)
{
log.debug("URLResourceLoader: Found '{}' at '{}'", name, root);
// save this root for later re-use
templateRoots.put(name, root);
break;
}
}
catch (IOException ioe)
{
if (rawStream != null)
{
try
{
rawStream.close();
}
catch (IOException e)
{
}
}
log.debug("URLResourceLoader: Exception when looking for '{}' at '{}'", name, root, ioe);
// only save the first one for later throwing
if (exception == null)
{
exception = ioe;
}
}
}
// if we never found the template
if (reader == null)
{
String msg;
if (exception == null)
{
msg = "URLResourceLoader: Resource '" + name + "' not found.";
}
else
{
msg = exception.getMessage();
}
// convert to a general Velocity ResourceNotFoundException
throw new ResourceNotFoundException(msg);
}
return reader;
}
private SizedConnection createCallableNetwork (final URL url, final int timeout) {
return new SizedConnection () {
private int contentLength = -1;
@Override
public int getContentLength() {
return contentLength;
}
@Override
public InputStream call () throws Exception {
URLConnection conn = url.openConnection ();
configureConnection(conn, timeout);
// handle redirection here
int redirCount = 0;
URLConnection redir = conn;
do {
conn = redir;
redir = checkRedirect(conn, timeout);
redirCount++;
} while (conn != redir && redirCount <= MAX_REDIRECTS);
if (conn != redir) {
throw new IOException("Too many redirects for " + url);
}
InputStream is = conn.getInputStream ();
contentLength = conn.getContentLength();
if (err.isLoggable(Level.FINE)) {
Map <String, List <String>> map = conn.getHeaderFields();
StringBuilder sb = new StringBuilder("Connection opened for:\n");
sb.append(" Url: ").append(conn.getURL()).append("\n");
for(String field : map.keySet()) {
sb.append(" ").append(field==null ? "Status" : field).append(": ").append(map.get(field)).append("\n");
}
sb.append("\n");
err.log(Level.FINE, sb.toString());
}
return new BufferedInputStream (is);
}
};
}
@Override
protected JSONObject doInBackground(Void... params) {
InputStream in = null;
try {
URLConnection conn = new URL(hooksDownloadUrl).openConnection();
conn.setConnectTimeout(40 * 1000 /* ms */);
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setReadTimeout(20 * 1000 /* ms */);
conn.setUseCaches(true);
if (conn instanceof HttpURLConnection) {
HttpURLConnection hConn = (HttpURLConnection) conn;
hConn.setChunkedStreamingMode(0);
hConn.setInstanceFollowRedirects(true);
hConn.setRequestMethod("GET");
} else {
Log.w(LOG_TAG, "Our connection is not java.net.HttpURLConnection but instead " + conn.getClass().getName());
}
conn.connect();
in = conn.getInputStream();
BufferedReader inReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder lineBuilder = new StringBuilder();
String line;
while ((line = inReader.readLine()) != null) {
lineBuilder.append(line);
}
return new JSONObject(lineBuilder.toString());
} catch (IOException | JSONException e) {
String em = e.getMessage();
Log.i(LOG_TAG, "The hook details could not be downloaded: " + (em == null ? "Unknown error" : em));
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
// no-op
}
}
}
}
public static final void httpSaveFile(String urlString, String savePath) throws IOException {
log.info("url string = " + urlString);
URL url = new URL(urlString);
URLConnection uc = url.openConnection();
InputStream input = uc.getInputStream();
byte[] buffer = new byte[4096];
int n = - 1;
OutputStream output = new FileOutputStream(savePath);
while ( (n = input.read(buffer)) != -1) {
output.write(buffer, 0, n);
}
output.close();
}