0

我有ShowPresentation.jsp从计算机上传 PPT 文件并通过 java servlet 将每张幻灯片转换为图像的方法。

那我需要NodeJS实时分享,怎么做?这是ShowPresentation.jsp内容。

<!DOCTYPE >
<html>
<head> 
    <script type="text/javascript">
        var current_slide_number = 1;
        function button(slideNumber){
            current_slide_number = current_slide_number + slideNumber;
            var image = document.getElementById('imageslide');
            var fileName = document.getElementById('hidFileName');
            image.src = 'Viewer?slideNumber='+current_slide_number+'&hidFileName='+fileName.value;
        }
    </script>
    <title>PowerPoint to JPEG</title>
</head>
<body>
    <div>
        <form method="post" action="Viewer" enctype="multipart/form-data" >
            <p >
                Upload a file:</p>
            <input type="file" name="datafile" size="40"> <input type="submit" value="Upload">
        </form>
    </div>
    <% if (request.getAttribute("fileName") != null) {
    %>
    <div id="slide-controls">
        <b>Please navigate through the slides</b>
                                                                                                                      
</div>

<div id="slide"><p align="center">
        <img id="imageslide" src="Viewer?hidFileName=<%=request.getAttribute("fileName")%>"></img>
    </p>
</div>

<% }%>
<input id="hidFileName" type="hidden" value="<%=request.getAttribute("fileName")%>"></input>

和这个 servlet 内容 Viewer.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class Viewer extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    int slideNumber = 1; 
    BufferedImage image = null;
    ServletOutputStream os = null;
    try{
        if (request.getParameter("hidFileName") != null){
            String fileName = request.getParameter("hidFileName");
        if(request.getParameter("slideNumber") != null)
            slideNumber = Integer.parseInt(request.getParameter("slideNumber"));
        image = getSlideAsImage(fileName, slideNumber);
        os = response.getOutputStream();
        ImageIO.write(image, "jpeg", os );
        }
        else {
            response.sendRedirect("StartPresentation.jsp");
        }   
    }
    catch (Exception e){
                if (os != null)
            os.close();
    }       
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String contentType = request.getContentType();
      String realPath = getServletContext().getRealPath("/");
        if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) 
        {
            DataInputStream in = new DataInputStream(request.getInputStream());
            int formDataLength = request.getContentLength();
            byte dataBytes[] = new byte[formDataLength];
            int byteRead = 0;
            int totalBytesRead = 0;
            while (totalBytesRead < formDataLength) {
                byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                totalBytesRead += byteRead;
            }
            String file = new String(dataBytes);
            String saveFile = file.substring(file.indexOf("filename=\"") + 10);
            saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1, saveFile.indexOf("\""));
            int lastIndex = contentType.lastIndexOf("=");
            String boundary = contentType.substring(lastIndex + 1, contentType.length());
            int pos;
            pos = file.indexOf("filename=\"");
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            int boundaryLocation = file.indexOf(boundary, pos) - 4;
            int startPos = ((file.substring(0, pos)).getBytes()).length;
            int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
            System.out.println("After File Name: "+ saveFile);
            String filePath = realPath + "/" +  saveFile;
            FileOutputStream fileOut = new FileOutputStream(filePath);
            fileOut.write(dataBytes, startPos, (endPos - startPos));
            fileOut.close();
            request.setAttribute("fileName", saveFile);
            getServletContext().getRequestDispatcher("/StartPresentation.jsp").forward(request, response);
        }
}
private BufferedImage getSlideAsImage(String fileName, int slideNumber) throws Exception
{
    BufferedImage image = null; 
    String realPath = getServletContext().getRealPath("/");
    String filePath = realPath + "/" + fileName;
    FileInputStream in = new FileInputStream(filePath);
    SlideShow ppt = new SlideShow(in);
    Slide[] slides = ppt.getSlides();
    Dimension pgsize = ppt.getPageSize();
    image = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = image.createGraphics();
    graphics.setPaint(Color.white);
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
    if (slideNumber < 1)
        slideNumber = 1;
    else if (slideNumber > slides.length)
        slideNumber = slides.length;
    slides[slideNumber - 1].draw(graphics);
    return image;
}

}

4

1 回答 1

1

为什么你需要nodejs?如果它们只是转换后的静态文件,为什么不使用静态资产服务器(例如 nginx)提供图像文件?

听起来您只需要一个静态断言服务器。如果您想使用节点来执行此操作,http-server将完成这项工作:

npm install -g http-server
cd my/assets
sudo http-server -p 80 &

然后在您的网络浏览器中访问http://localhost 。

于 2012-05-03T12:13:40.710 回答