我可以使用 Java / JSP 代码进行文件上传、上传视频吗?

发布于 2024-11-05 01:18:00 字数 1534 浏览 0 评论 0原文

我的站点是用 JSP 页面和 Java Servlet 设计的。

在我的网站上,我已经制定了允许用户上传文件的流程。这些文件存储在我的服务器上的文件夹中。

现在,我想让用户选择上传视频并将视频存储在我的服务器上,就像我处理图像文件一样。

问题
1) 我可以使用已有的代码来上传文件吗?如果是这样,我需要添加或更改什么?

2) 如果我无法使用我的代码,如何编写视频上传代码?

3) 如何在 Html 或 JSP 页面上显示视频?


编辑 - 更新

上传文件的代码片段 - 并将其重命名为 newvideo.avi

         while(it.hasNext()) {                                 

                FileItem item = (FileItem)it.next(); // Create a FileItem object to access the file.    
                String fieldValue = item.getName();                                             

                if(!item.isFormField()) {// formField                               

                      File f = new File(fieldValue);                        

                    // Prepare streams.
                    fs = new FileInputStream(f);
                    fos = new FileOutputStream(complete_path+"\\newvideo.avi");

                        // Write file contents to response.
                        byte[] buffer = new byte[(int)f.length()];
                        int length;
                        while ((length = fs.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }

                    fs.close();
                    fos.close();                        

              }//formField                                                   
            }//while

My site is designed with JSP pages and Java Servlets.

On my site, I already have the process in place for allowing users to upload files. The files get stored in a folder on my server.

Now I would like to give users the option of uploading a video and store the video on my server, just as I do with the image files.

Questions
1) Can I use my code I already have for uploading files? If so, what do I need to add or change?

2) If I can't use my code, how do I code for the upload of a video?

3) How do I display the video on an Html or JSP page?


Edit - Update

A snippet of the code for uploading a file - and renaming it newvideo.avi

         while(it.hasNext()) {                                 

                FileItem item = (FileItem)it.next(); // Create a FileItem object to access the file.    
                String fieldValue = item.getName();                                             

                if(!item.isFormField()) {// formField                               

                      File f = new File(fieldValue);                        

                    // Prepare streams.
                    fs = new FileInputStream(f);
                    fos = new FileOutputStream(complete_path+"\\newvideo.avi");

                        // Write file contents to response.
                        byte[] buffer = new byte[(int)f.length()];
                        int length;
                        while ((length = fs.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }

                    fs.close();
                    fos.close();                        

              }//formField                                                   
            }//while

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甚是思念 2024-11-12 01:18:00

请参阅JSP上传和下载视频

但请记住文件上传的基本原理servlet 保持不变,因此您可能会发现值得花时间阅读本文 - 在 JSP/Servlet 中管理文件上传

您提到的处理图像上传的方式不是很干净,也不是很有效。您正在用 Java 创建对象,然后将其写入磁盘。不需要这种开销,您可以简单地将输入流写入磁盘并获得相同的结果。这也使得接受任何类型文件的上传变得微不足道。

See JSP Uploading and downloading Video

But keep in mind that the basic principle for a file upload to a servlet remains the same so you might find it worth your time to read this as well - Managing file uploads in JSP/Servlets

The way you've mentioned you're handling image uploads isn't very clean nor is it very efficient. You're creating objects in Java that you're then to writing to disk. There is no need for this overhead, you could simply write the input stream to the disk and get the same results. This also makes it trivial to accept uploads of any kind of file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文