【问题标题】:How can I set setContentType("text/html") or setContentType("text/plain") dynamically如何动态设置 setContentType("text/html") 或 setContentType("text/plain")
【发布时间】:2020-01-12 10:12:55
【问题描述】:

我需要修改读取文本文件的代码,以动态读取文本或html文件。

现在代码只使用

response.setContentType("text/plain");

因为文件是以文本格式保存的。但我想保存为 html 格式来管理所有标签并有一个更好的视图,但是如果我在

中修改

response.setContentType("text/html");

所有保存为文本的文件都有错误的查看器

我的代码是:

package uk.co.mycode.fax.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import uk.co.mycode.fax.dao.mycodeFaxDAO;
import uk.co.mycode.fax.domain.Image;
import uk.co.morpheus.logging.Logger;

public class FaxImageRequest
  extends HttpServlet
{
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { performTask(request, response); }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { performTask(request, response); }

  public void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String url = request.getParameter("U");
    if (url == null || url.length() < 1) {
      url = request.getParameter("URL");
    }
    Logger.log(4, getClass().getName(), "<**** Entered FaxImageRequest (" + url + ") ****>");
    try {
      Cookie[] cookies = request.getCookies();
      String userId = "";
      if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
          if (cookies[i].getName().equals("IMPS3IAuserid")) {
            userId = cookies[i].getValue();

            break;
          } 
        } 
      }
      if (userId.length() < 1) {
        userId = "NOT LOGGED ON";
      }

      mycodeFaxDAO dao = mycodeFaxDAO.getInstance();

      Image image = dao.getImage(url);

      Logger.log(4, getClass().getName(), "Image: " + image);

      if (image != null) {
        ServletOutputStream out = response.getOutputStream();
        if (image.type.toLowerCase().startsWith("f")) {
            response.setContentType("image/tiff");
        } else {
//----------------------------------------------------------------------------

               response.setContentType("text/plain");

//----------------------------------------------------------------------------      
        } 

        for (int i = 0; i < image.bytes.length; ) { out.write(image.bytes, i, (image.bytes.length - i > 4096) ? 4096 : (image.bytes.length - i)); i += 4096; }

        dao.updateImageArchive(userId, request.getParameter("U"));
      } else {

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.println("<HTML>");
        pw.println("<BODY>");
        pw.println("<B>No image found</B>");
        pw.println("</BODY>");
        pw.println("</HTML>");
        pw.close();
      } 
    } catch (Throwable th) {
      Logger.log(1, getClass().getName(), "Error during image read or update:" + th.getMessage());
      th.printStackTrace();
    } 

    Logger.log(4, getClass().getName(), "<**** Finished FaxImageRequest ****>");
  }
}

我试着写这个:

if (response.getContentType() == null) {
                response.setContentType("text/html");
              } else {
                    response.setContentType("text/plain");
              }

但它始终为空。 感谢您的支持。

【问题讨论】:

  • 如何知道文件是文本文件还是 HTML 文件?
  • 这是我的问题。我需要先知道。所有新文件都是 html,但所有旧文件都是文本
  • 问题没有足够的信息让我们帮助解决这个问题。它确实有很多不相关的代码,使情况混乱。无论如何,“文件”是否有名称,例如带有.txt 和.html 之类的扩展名?在该代码中甚至看不到任何代表“文件”的内容,除非Image 是一个 tiff/text/html“文件”,在这种情况下,它是一个名称非常糟糕的类。

标签: java html servlets text mime-types


【解决方案1】:

如果我可以提出您的“托管平台”,正确的答案是 - 通常在托管平台中更改此类设置。我使用 GCS(谷歌云服务器)托管了三个不同的网络域,因为它(大部分)是免费的。

在 Google Cloud 中,当您保存文件时,它通常会根据文件的扩展名 - '.txt' 或 '.html' 识别您保存的文件类型(Content-Type)。可以使用鼠标在 Google 的存储桶文件浏览器 GUI 中更改 Content-Type 设置。也可以使用 GSUTIL 命令行程序在命令行中手动更改。

您可以使用 Java 的 shell 执行库(用于调用 UNIX Shell 命令的 Java 标准库例程)从 Java 调用 GSUTIL。这就是我的工作......

...但您甚至可以下载 Google 的 Java Jar 文件以访问 GCS,并对 GCS 进行基于 Java 的调用,以执行诸如更改存储桶文件的内容类型之类的操作...。

如果您使用的是.MSFT Azure、GoDaddy 或其他东西,情况会有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2014-02-14
    • 2012-05-19
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    相关资源
    最近更新 更多