java - Jsp 不显示 swf 文件

标签 java jsp tomcat servlets

我有一个带有 JavaEE(Tomcat、Jsp、Servlet)的 Web 项目

我想在我的 Jsp 页面 (game.jsp) 中显示一个 SWF。为此,我需要一个 Servlet,它是这样的:

package src;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.SystemEnviroement;

/**
 * Servlet implementation class ImageServlet
 */
@WebServlet("/ImageServlet")
public class ImageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


/**
 * @see HttpServlet#HttpServlet()
 */
public ImageServlet() {
    super();
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    ServletContext sc = getServletContext();
    String imageName = request.getParameter("imageName");


    SystemEnviroement env = new SystemEnviroement();

        String filename = env.gameFolder + "/" + imageName;

        // Get the MIME type of the image
        String mimeType = sc.getMimeType(filename);
        if (mimeType == null) {
            // sc.log("Could not get MIME type of " + filename);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        // Set content type
        response.setContentType(mimeType);

        // Set content size
        File file = new File(filename);
        response.setContentLength((int) file.length());

        // Open the file and output streams
        FileInputStream in = new FileInputStream(file);
        OutputStream out = response.getOutputStream();

        // Copy the contents of the file to the output stream
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }

        in.close();
        out.close();


}

我的 game.jsp 是这样的:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="util.*"%>
<%@ page import="constants.*"%>
<%@ page import="java.io.IOException"%>

<!DOCTYPE html>
<html >
<head>
<jsp:include page='header.jsp'/>
<% 
String gamename = (String) request.getAttribute("javax.servlet.forward.request_uri");
int index_name = gamename.lastIndexOf("/");

gamename = gamename.substring(index_name+1,gamename.length());



%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


</head>
<body style="background-color:#630592; 
      background-repeat:no-repeat;"
>


<div class="body">

        <%

        try {
            //SystemEnviroment wird im Konstruktor gesetzt
            SystemEnviroement en = new SystemEnviroement();
            String datei = en.imageViewPath + gamename +".swf";
    %>



    <div class=gameswf>
        <a> <embed src='<%=datei %>'  > </embed> </a>
    </div>


    <%  
    }catch(IOException e){
        e.printStackTrace();
    }
    %>

</div>

</body>
</html>

所以我调试了我的项目,一切都很好。但是调用 Servlet 后,game.jsp 不显示 SWF 文件。

htmltext(源代码)看起来也不错,但是 game.jsp 没有显示 SWF 文件:

<div class=gameswf>
        <a> <embed src='http://localhost:8080/Game/imageView?imageName=3-pandas.swf'>   </embed> </a>
    </div>

如果我在运行的网络项目中调用此 URL “http://localhost:8080/Game/imageView?imageName=3-pandas.swf”,我可以出售 SWF 文件和所有很好。

您知道为什么 jsp 页面不显示我的 SWF 文件吗?如果我去 Internet Explorer 加载项,我还可以看到正在加载 Shockwave Flash 对象。

感谢您的帮助!

这是来自开发者工具的 ie 11 网络监控功能的图像:

enter image description here

最佳答案

我建议您创建一个 html 文件(带有 html 扩展名)并编写您希望从 jsp 生成的代码。 一旦您使 swf 在 html 文件中工作,开发适当的 jsp 代码以生成该 html 文件将是小菜一碟。

关于java - Jsp 不显示 swf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27479306/

相关文章:

java - 模拟 bean 的依赖项的 NoSuchBeanDefinitionException

java - 如何修复 Spring Boot 中的 "Application failed to start"并要求定义 bean

java - 如何从java中的所有 Activity session 中清除 session 属性?

java - 从外部 jsp 页面显示对话框(Struts2、java)

java - Tomcat启动失败,服务器8080端口已被占用

Java - AWT 从 1.4 到 1.5 的差异(适用于 Unix 和 Windows)

java - 模拟类,Java

java - LDAP LDIF Java 解析和换行

java - 自定义 UserdetailsS​​ervice 中的 @EJB 注入(inject)(实现 Spring Security UserDetailsS​​ervice)

javascript - 使用 Jquery 使嵌套 forEach 循环下的每个部分可折叠