Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示多个内容类型httpresponse_Java_Httpclient_Httpresponse - Fatal编程技术网

Java 显示多个内容类型httpresponse

Java 显示多个内容类型httpresponse,java,httpclient,httpresponse,Java,Httpclient,Httpresponse,我试图在同一页面中显示多种内容类型(文本/html和图像/png内联图像)。我在磁盘/数据库中存储了图像内容和文本/html内容以及所有标题信息(基本上假设我可以使用输入流读取文本/html和图像的内容)。我想创建一个包含text/html部分和内联图像部分的HttpResponse。我遇到了一些关于HttpClient/MultipartEntity的引用。因此,我尝试了一个示例代码,用MultipartEntity显示一个图像(保存在我的磁盘中),但我在页面中看到的都是胡言乱语。我在构建路径

我试图在同一页面中显示多种内容类型(文本/html和图像/png内联图像)。我在磁盘/数据库中存储了图像内容和文本/html内容以及所有标题信息(基本上假设我可以使用输入流读取文本/html和图像的内容)。我想创建一个包含text/html部分和内联图像部分的HttpResponse。我遇到了一些关于HttpClient/MultipartEntity的引用。因此,我尝试了一个示例代码,用MultipartEntity显示一个图像(保存在我的磁盘中),但我在页面中看到的都是胡言乱语。我在构建路径中引用的jar是apache-mime4j-0.6.jar、httpcore-4.0.1.jar、httpmime-4.0.jar。我正在使用ApacheTomcat服务器。下面是示例代码

import java.io.*;
import java.nio.charset.Charset;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;


public class MyHelloWorldServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {

        ServletOutputStream out = response.getOutputStream();
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName("UTF-8"));
        //File file = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\test.msg");
        File file = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
        InputStream in1 = new FileInputStream(file);
        InputStreamBody fileBody1 = new InputStreamBody(new FileInputStream(file), "Chrysanthemum.jpg");
        entity.addPart("part1", fileBody1);
        entity.writeTo(out);

    }
}

还有人能告诉我是否可以以类似的方式添加多内容类型和显示的部分吗?

也许我没有正确地理解您。看起来您想要的是创建一个具有相同映射的servlet来处理不同类型的请求?我说得对吗?如果这是正确的,为什么不根据数据库中的标题更改内容类型呢

            response.setContentType("image/jpg");
            response.setContentType("text/html");
然后,根据您想要提供的服务,您可以使用图像或文件: 对于html:

  response.setContentType("text/html");   
  PrintWriter out = res.getWriter();  
  out.println("<html>....</html>");
  out.close();

谢谢你的回复。我不是在寻找一个处理不同类型内容类型的servlet,而是一个混合内容类型的servlet。您的示例正确地将内容类型设置为image/png,如果我只想显示image,但我的页面将包含多个mime部分,比如text/html,后面跟着image/png。下面是servlet应返回的示例响应标题-------------------u=\ u NextPart\u 001\u 01CDA60B.AB4C0472内容类型:多部分/可选;boundary=“----下一部分内容类型:文本/纯文本;charset=“us ascii”内容传输编码:引用可打印样本包含多部分/备选方案;和文本/纯文本;内容类型。类似地,我希望显示多个text/html和image/pngJust来添加..servlet应该返回一个类似于中投票最多的响应
    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try{

            stream = response.getOutputStream();
            File mp3 = new File("path/tofile ");

            if(request.getSession().getAttribute( "path" )!=null){      
                 mp3 = new File(request.getSession().getAttribute( "path" ).toString());
                 request.getSession().setAttribute("path", null);
            } 

            response.setContentType("image/jpg");        
            response.setContentLength( (int) mp3.length() );

            FileInputStream input = new FileInputStream(mp3);
            buf = new BufferedInputStream(input);
            int readBytes = 0;

            while((readBytes = buf.read()) != -1)
               stream.write(readBytes);

   } catch (IOException ioe){       
      throw new ServletException(ioe.getMessage());           
   } finally {
       if(stream != null)
           stream.close();
       if(buf != null)
           buf.close();
   }