【问题标题】:Receiving varying lengths in request.getInputStream for the same file在 request.getInputStream 中为同一个文件接收不同的长度
【发布时间】:2012-01-23 16:46:57
【问题描述】:

我的网站上有一个文件上传器,因为我不能使用 php 我正在使用 jsp 页面。 我的主页使用隐藏的 iframe 将数据发布到处理上传的第二个 jsp 页面。但是上传的图像总是损坏,更具体地说,它的大小比原始文件大。 任何提示或提示将不胜感激。 主页代码:

<form id="uploadForm">
    <input type="file" name="datafile" /></br>
    <input type="button" value="upload" onClick="fileUpload(document.getElementById('uploadForm'),'single_upload_page.jsp','upload'); return false;" >
</form>

关于表单的fileUpload代码:

form.setAttribute("target","upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method","post");
form.setAttribute("enctype","multipart/form-data");
form.setAttribute("encoding","multipart/form-data");

// Submit the form...
form.submit();

处理上传的代码:

DataInputStream in = new DataInputStream(request.getInputStream());
int dataLength = request.getContentLength();

由于 dataLength 的大小不同,我假设 request.getInputStream 接收到额外的数据。

我只发布了我认为重要的代码,如果我需要发布更多信息或您需要更多信息,请随时询问。

【问题讨论】:

    标签: java javascript file jsp upload


    【解决方案1】:

    简单的请求

    问题是 request.getContentLength() 给出了整个请求的长度,包含标头和所有。

    您必须搜索 Content-Length 标头值,将其转换为 Long ,这是正确的大小。

    如果你不能得到它(它可能不可用),只需消耗整个输入流。但当然,您不会知道文件大小。

    多部分请求

    无论如何...由于您的表单是多部分/表单数据,您应该使用一些库来解析所有不同的部分,找到您需要的部分(文件部分)并阅读它。你可以使用commons-fileupload。

    现实生活中的样本

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp)
            throws ServletException, IOException 
    {
            // (....)
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload sfu = new ServletFileUpload(factory);
            FileItemIterator it = sfu.getItemIterator(request);
            // TAKE THE FIRST PART FROM REQUEST (HERE COMES THE FILE)
            if (it.hasNext())
            {
                FileItemStream fis = it.next();
                // grab data from fis (content type, name)
                ...fis.getContentType()...
                ...fis.getName()...
                // GET CONTENT LENGTH SEARCH FOR THE LENGTH HEADER
                ...getContentLength(fis.getHeaders(), request)...
                // here I use an own method to process data
                // but FileItemStream has an openStream method
                FileItem item = processUpload(factory, fis, uploadInfo);
                (....)
            }
    
    
    
    private long getContentLength(FileItemHeaders pHeaders, HttpServletRequest request)
    {
        try
        {
            return Long.parseLong(pHeaders.getHeader("Content-length"));
        }
        catch (Exception e)
        {
                        // if I can't grab the value return an approximate (in my case I don't care)
            return request.getContentLength();
        }
    }
    

    【讨论】:

    • 谢谢,通过使用子字符串,我能够找出 byte[] 边界的 indexex,它可能不是最好的方法,但它会让上传工作
    • 好吧。不是那么健壮,但如果它有效...... :) 当你不那么忙的时候试试那个库。使用起来并不难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多