【问题标题】:a problem about urlfetch over 1M in app engine应用引擎中 urlfetch 超过 1M 的问题
【发布时间】:2010-12-12 12:55:50
【问题描述】:

要在应用引擎中 fetch() 超过 1M,我使用范围标头,然后结合这些部分。和我的代码:

int startpos=0;
int endpos;
int seg=1;
int len=1;
while(len>0){
endpos=startpos+seg;
httpConn = (HttpURLConnection) u.openConnection();
httpConn.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");

con.setRequestProperty("Range", "bytes=" + startpos + "-" + endpos);  
con.connect();
InputStream in=con.getInputStream();

len=con.getContentLength();
byte[] b=new byte[len];
in.read(b, 0, len);

startpos+=len; 

} 但是当它进入“InputStream in=con.getInputStream();”时,它的调试是“URL Fetch Response too large questions” 所以我不知道这些代码有什么问题。 还有其他方法可以 fetch() 超过 1M 吗?

【问题讨论】:

  • 该代码甚至不应该编译,因为您已经两次声明了b...
  • 逐块请求它而不是请求一次并逐块读取有什么好处吗?请注意,您的代码不会编译,因为您已经声明了两次 b
  • App engine Urlfetch over 1M? 的可能重复项

标签: java google-app-engine urlfetch


【解决方案1】:

并非所有 HTTP 服务器都支持范围请求,尤其是在涉及提供动态内容的框架时 - 它们会简单地忽略 Range 标头并将整个响应发送给您。

不过,最近发布的 1.4.0 将 URLFetch 响应限制增加到 32MB,因此您不再需要这样做。

【讨论】:

  • 谢谢。我更新了我的版本,现在是1.4.0,但是当我运行它时,它仍然不起作用。它的调试是“com.google.appengine.api.urlfetch .ResponseTooLargeException:来自 url localhost:8888/dem.bil 的响应太大。”这些是我的代码:try { URLConnection a = url.openConnection();输入流 b = a.getInputStream(); int len = a.getContentLength(); if (len
【解决方案2】:

我遇到了同样的问题,并编写了一个小类来使用 HTTP 范围参数在 Appengine 上模拟输入流。它允许您以面向行的方式读取大于限制的文件。我将其附在下面,尽管您可能需要根据您的目的对其进行调整:

package com.theodorebook.AEStreamer;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.logging.Logger;

/**
 * A class to simulate a stream in appengine, which insists on downloading
 * an entire URL before letting you do anything with it.  This enables one
 * to read files larger than the size limits.
 * 
 * @author Theodore Book (theodorebook at gmail dot com)
 *
 */
public class AEStreamer {
    private static final int BITE_SIZE = 0x10000;   //How big a chunk to grab at a time
    private static final byte TERMINATOR = '\n';    //String terminator

    private int mCurrentPosition = 0;   //The current position in the file
    private int mOffset = -1;   //The offset of the current block
    private long mValidBytes = 0;   //The number of valid bytes in the chunk
    private byte[] mChunk = new byte[BITE_SIZE];
    private boolean mComplete = false;
    private String mURL;

    private static final Logger log = Logger.getLogger(AEStreamer.class.getName());

    public AEStreamer(String url) {
        mURL = url;
    }

    /**
     * Returns the next line from the source, or null on empty
     * @return
     */
    public String readLine() {
        String line = "";

        //See if we have something to read
        if (mCurrentPosition >= mOffset + mValidBytes) {
            if (mComplete)
                return null;
            readChunk();
        }
        if (mValidBytes == 0)
            return null;

        //Read until we reach a terminator
        int endPtr = mCurrentPosition - mOffset;
        while (mChunk[endPtr] != TERMINATOR) {
            endPtr++;

            //If we reach the end of the block
            if (endPtr == mValidBytes) {
                line += new String(Arrays.copyOfRange(mChunk, mCurrentPosition - mOffset, endPtr));
                mCurrentPosition += (endPtr - mCurrentPosition + mOffset);
                if (mComplete) {
                    return line;
                } else {
                    readChunk();
                    endPtr = mCurrentPosition - mOffset;
                }
            }
        }
        line += new String(Arrays.copyOfRange(mChunk, mCurrentPosition - mOffset, endPtr));
        mCurrentPosition += (endPtr - mCurrentPosition + mOffset);
        mCurrentPosition++;
        return line;
    }

    /**
     * Reads the next chunk from the server
     */
    private void readChunk() {
        if (mOffset < 0)
            mOffset = 0;
        else
            mOffset += BITE_SIZE;

        try {
            URL url = new URL(mURL);
            URLConnection request = url.openConnection();
            request.setRequestProperty("Range", "bytes=" + (mOffset + 1) + "-" + (mOffset + BITE_SIZE)); 
            InputStream inStream = request.getInputStream();
            mValidBytes = inStream.read(mChunk);
            inStream.close();
        } catch (Exception e) {
            log.severe("Unable to read " + mURL + ": " + e.getLocalizedMessage());
            mComplete = true;
            mValidBytes = 0;
            return;
        }

        if (mValidBytes < BITE_SIZE)
            mComplete = true;

        //log.info("Read " + mValidBytes + " bytes");
    }
}

【讨论】:

    猜你喜欢
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 2014-03-09
    • 2019-03-16
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多