Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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 FileServlet已在下载中损坏视频_Java_Jsf - Fatal编程技术网

Java FileServlet已在下载中损坏视频

Java FileServlet已在下载中损坏视频,java,jsf,Java,Jsf,嗯,我试图使用FileServlet从我的Web服务器(ApacheTomcat)下载视频,但是这个视频坏了。我知道视频是可以的,因为如果我用FileZilla下载,一切都很好 见我的班级: public class FileServlet extends HttpServlet { // Constants // ----------------------------------------------------------------------------------

嗯,我试图使用FileServlet从我的Web服务器(ApacheTomcat)下载视频,但是这个视频坏了。我知道视频是可以的,因为如果我用FileZilla下载,一切都很好

见我的班级:

public class FileServlet extends HttpServlet {

    // Constants
    // ----------------------------------------------------------------------------------

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

    // Properties
    // ---------------------------------------------------------------------------------

    private String filePath;

    // Actions
    // ------------------------------------------------------------------------------------

    private static abstract class InnerFacesContext extends FacesContext {

    protected static void setFacesContextAsCurrentInstance(
            FacesContext facesContext) {
        FacesContext.setCurrentInstance(facesContext);
    }

    private InnerFacesContext() {
    }
    }

    public void init() throws ServletException {

    // In a Windows environment with the Applicationserver running on the
    // c: volume, the above path is exactly the same as "c:\files".
    // In UNIX, it is just straightforward "/files".
    }

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

    this.filePath = getFacesContext(request, response).getExternalContext()
            .getInitParameter("tmpDirectory");

    // Get requested file by path info.
    String requestedFile = request.getPathInfo();

    // Check if file is actually supplied to the request URI.
    if (requestedFile == null) {
        // Do your thing if the file is not supplied to the request URI.
        // Throw an exception, or send 404, or show default/warning page, or
        // just ignore it.
        response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
        return;
    }

    // Decode the file name (might contain spaces and on) and prepare file
    // object.
    File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8"));

    // Check if file actually exists in filesystem.
    if (!file.exists()) {
        // Do your thing if the file appears to be non-existing.
        // Throw an exception, or send 404, or show default/warning page, or
        // just ignore it.
        response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
        return;
    }

    // Get content type by filename.
    String contentType = getServletContext().getMimeType(file.getName());

    // If content type is unknown, then set the default value.
    // For all content types, see:
    // http://www.w3schools.com/media/media_mimeref.asp
    // To add new content types, add new mime-mapping entry in web.xml.
    if (contentType == null) {
        contentType = "application/octet-stream";
    }

    // Init servlet response.
    response.reset();
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setContentType(contentType);
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

    // Prepare streams.
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        // Open streams.
        input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
        output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

        // Write file contents to response.
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
        }
    } finally {
        // Gently close streams.
        close(output);
        close(input);
    }
    }

    // Helpers (can be refactored to public utility class)
    // ----------------------------------------

    private static void close(Closeable resource) {
    if (resource != null) {
        try {
        resource.close();
        } catch (IOException e) {
        // Do your thing with the exception. Print it, log it or mail
        // it.
        e.printStackTrace();
        }
    }
    }

    protected FacesContext getFacesContext(HttpServletRequest request,
        HttpServletResponse response) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (facesContext == null) {
        FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
            .getFactory("javax.faces.context.FacesContextFactory");
        LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
            .getFactory("javax.faces.lifecycle.LifecycleFactory");
        javax.faces.lifecycle.Lifecycle lifecycle = lifecycleFactory
            .getLifecycle("DEFAULT");
        facesContext = contextFactory.getFacesContext(request.getSession()
            .getServletContext(), request, response, lifecycle);
        InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
        javax.faces.component.UIViewRoot view = facesContext
            .getApplication().getViewHandler()
            .createView(facesContext, "");
        facesContext.setViewRoot(view);
    }
    return facesContext;
    }

}

PS:这个类来自BalusC博客。

您是否尝试通过调用
flush
方法刷新缓冲区?视频是如何被破坏的?文件是如何创建的?是空的吗?它在那里,但比预期的视频小吗?另外,您是否考虑过使用ApacheCommons IOUtils来复制流?这会让你的生活更轻松@妈的,我该怎么做?@hugh,我的文件创建的大小相同,但我无法在VLC播放器中查看。