简体   繁体   English

从GridFS读取时不显示非ASCII字符

[英]Non-ASCII Characters Not Displayed When Reading From GridFS

I am uploading files (of different content types) using Apache fileupload API as follows: 我使用Apache fileupload API上传文件(具有不同的内容类型),如下所示:

FileItemFactory factory = getFileItemFactory(request.getContentLength());
ServletFileUpload uploader = new ServletFileUpload(factory);
uploader.setSizeMax(maxSize);
uploader.setProgressListener(listener);

List<FileItem> uploadedItems = uploader.parseRequest(request);

... saving files to GridFS using the following method: ...使用以下方法将文件保存到GridFS:

public String saveFile(InputStream is, String contentType) throws UnknownHostException, MongoException {
    GridFSInputFile in = getFileService().createFile(is);
    in.setContentType(contentType);
    in.save();
    ObjectId key = (ObjectId) in.getId();
    return key.toStringMongod();
}

... calling saveFile() as follows: ...如下调用saveFile():

saveFile(fileItem.getInputStream(), fileItem.getContentType())

and reading from GridFS using the following method: 并使用以下方法从GridFS中读取:

public void writeFileTo(String key, HttpServletResponse resp) throws IOException {
    GridFSDBFile out = getFileService().findOne(new ObjectId(key));
    if (out == null) {
        throw new FileNotFoundException(key);
    }
    resp.setContentType(out.getContentType());
    out.writeTo(resp.getOutputStream());
}

My servlet code to download the file: 我的servlet代码下载文件:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String uri = req.getRequestURI();

    String[] uriParts = uri.split("/");  // expecting "/content/[key]"

    // third part should be the key
    if (uriParts.length == 3) {
        try {
            resp.setDateHeader("Expires", System.currentTimeMillis() + (CACHE_AGE * 1000L));
            resp.setHeader("Cache-Control", "max-age=" + CACHE_AGE);
            resp.setCharacterEncoding("UTF-8");

            fileStorageService.writeFileTo(uriParts[2], resp);
        }
        catch (FileNotFoundException fnfe) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
        catch (IOException ioe) {
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
    else {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}

However; 然而; all non-ASCII characters are displayed as '?' 所有非ASCII字符都显示为“?” on a web page with encoding set to UTF-8 using: 在网页上使用以下代码将编码设置为UTF-8:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

Any help would be greatly appreciated! 任何帮助将不胜感激!

Apologies for taking your time! 抱歉,抽出宝贵的时间! This was my mistake. 这是我的错 There is nothing wrong with the code or GridFS. 代码或GridFS没有错。 My test file's encoding was wrong. 我的测试文件的编码错误。

resp.setContentType("text/html; charset=UTF-8");

Reason: only content type, together with a binary InputStream are passed on. 原因:仅传递内容类型以及二进制InputStream。

public void writeFileTo(String key, HttpServletResponse resp) throws IOException {
    GridFSDBFile out = getFileService().findOne(new ObjectId(key));
    if (out == null) {
        throw new FileNotFoundException(key);
    }
    resp.setContentType(out.getContentType()); // This might be a conflict
    out.writeTo(resp.getOutputStream());

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM