Java 从浏览器上传多个文件的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/582126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to upload multiple files from a browser
提问by Sébastien Nussbaumer
I'm working on a web application. There is one place where the user can upload files with the HTTP protocol. There is a choice between the classic HTML file upload control and a Java applet to upload the files.
我正在开发一个网络应用程序。有一处用户可以使用 HTTP 协议上传文件。可以选择经典的 HTML 文件上传控件和 Java 小程序来上传文件。
The classic HTML file upload isn't great because you can only select one file at a time, and it's quite hard to get any progress indication during the actual upload (I finally got it using a timer refreshing a progress indicator with data fetched from the server via an AJAX call). The advantage: it's always working.
经典的 HTML 文件上传不是很好,因为您一次只能选择一个文件,并且在实际上传过程中很难获得任何进度指示(我最终使用计时器刷新进度指示器并使用从服务器通过 AJAX 调用)。优点:它始终有效。
With the Java applet I can do more things: select multiple files at once (even a folder), compress the files, get a real progress bar, drag'n'drop files on the applet, etc...
BUT there are a few drawbacks:
使用 Java 小程序我可以做更多的事情:一次选择多个文件(甚至是一个文件夹)、压缩文件、获得真正的进度条、将文件拖放到小程序上等等......
但是有一些缺点:
- it's a nightmare to get it to work properly on Mac Safari and Mac Firefox (Thanks Liveconnect)
- the UI isn't exactly the native UI and some people notice that
- the applet isn't as responsive as it should (could be my fault, but everything looks ok to me)
- there are bugs in the Java
UrlConnection
class with HTTPS, so I use the Apache common HTTP client to do the actual HTTP upload. It's quite big a package and slows down the download of the .jar file - the Apache common HTTP client has sometimes trouble going through proxies
- the Java runtime is quite big
- 让它在 Mac Safari 和 Mac Firefox 上正常工作是一场噩梦(感谢 Liveconnect)
- 用户界面并不完全是原生用户界面,有些人注意到
- 小程序没有应有的响应(可能是我的错,但在我看来一切正常)
UrlConnection
使用HTTPS的Java类存在bug ,所以我使用Apache通用HTTP客户端来做实际的HTTP上传。这是一个相当大的包,减慢了 .jar 文件的下载速度- Apache 通用 HTTP 客户端有时无法通过代理
- Java 运行时相当大
I've been maintaining this Java applet for a while but now I'm fed up with all the drawbacks, and considering writing/buying a completely new component to upload theses files.
我一直在维护这个 Java 小程序一段时间,但现在我厌倦了所有的缺点,并考虑编写/购买一个全新的组件来上传这些文件。
Question
题
If you had the following requirements:
如果您有以下要求:
- upload multiple files easily from a browser, through HTTP or HTTPS
- compress the files to reduce the upload time
- upload should work on any platform, with native UI
- must be able to upload huge files, up to 2gb at least
- you have carte blanche on the technology
- 通过 HTTP 或 HTTPS 从浏览器轻松上传多个文件
- 压缩文件以减少上传时间
- 上传应该适用于任何平台,具有原生 UI
- 必须能够上传大文件,至少可达 2GB
- 你可以全权决定技术
What technology/compontent would you use?
你会使用什么技术/组件?
Edit :
编辑 :
- Drag'n'Drop of files on the component would be a great plus.
- It looks like there are a lot of issues related to bugs with the Flash Player (swfupload known issues). Proper Mac support and upload through proxies with authentication are options I can not do without. This would probably rule out all Flash-based options :-( .
- I rule out all HTML/Javascript-only options because you can't select more than one file at a time with the classic HTML control. It's a pain to click n-times the "browse" button when you want to select multiple files in a folder.
- 在组件上拖放文件将是一个很大的优势。
- 看起来有很多与 Flash Player 的错误相关的问题(swfupload 已知问题)。适当的 Mac 支持和通过带有身份验证的代理上传是我不能没有的选项。这可能会排除所有基于 Flash 的选项:-(。
- 我排除了所有 HTML/Javascript-only 选项,因为使用经典 HTML 控件一次只能选择一个文件。当您想在一个文件夹中选择多个文件时,点击 n 次“浏览”按钮会很痛苦。
采纳答案by Sébastien Nussbaumer
OK this is my take on this
好的,这是我对此的看法
I did some testing with swfupload, and I have my previous experience with Java, and my conclusion is that whatever technology is used there is no perfect solution to do uploads on the browser: you'll always end up with bugs when uploading huge files, going through proxies, with ssl, etc...
我用 swfupload 做了一些测试,我之前有 Java 的经验,我的结论是,无论使用什么技术,都没有完美的解决方案来在浏览器上上传:上传大文件时,你总是会遇到错误,通过代理,使用 ssl 等...
BUT :
但 :
- a flash uploader (a la swfupload) is really lightweight, doesn't need authorization from the user and has a native interface which is REALLY cool, me thinks
- a java uploader needs authorization but you can do whatever you want with the files selected by the user (aka compression if needed), and drag and drop works well. Be prepared for some epic bugs debuggin' though.
- I didn't get a change to play with Silverlight as long as I'd like maybe that's the real answer, though the technology is still quite young so ... I'll edit this post if I get a chance to fiddle a bit with Silverlight
- Flash 上传器(a la swfupload)真的很轻量级,不需要用户授权,并且有一个非常酷的本机界面,我认为
- java 上传器需要授权,但您可以对用户选择的文件做任何您想做的事情(如果需要,也可以压缩),并且拖放效果很好。不过,请为一些史诗般的错误调试做好准备。
- 只要我愿意,我就没有改变使用 Silverlight,也许这就是真正的答案,尽管这项技术还很年轻,所以......如果我有机会摆弄一下,我会编辑这篇文章与银光
Thanks for all the answers !!
感谢所有的答案 !!
回答by Dave Swersky
回答by adam
There are a number of free flash components that exist with nice multiple file upload capability. They make use of ActionScripts FileReference class with a PHP (or whatever) receiver on the server side. Some have recently broken with the launch of FP10 but I know for certain that swfuploadwill work :)
有许多免费的 Flash 组件具有很好的多文件上传功能。他们在服务器端使用带有 PHP(或其他)接收器的 ActionScripts FileReference 类。有些人最近因 FP10 的推出而中断,但我确信swfupload会起作用:)
Hope this helps!
希望这可以帮助!
回答by Jay
You can upload multiple files with HTTP forms as well, as Dave already pointed out, but if you're set on using something beyond what HTTP and Javascript offers I would heavily consider Flash. There are even some pre-existing solutions for it such as MultiPowUploadand it offers many of the features you're looking for. It's also easier to obtain progress information using a Flash client than with AJAX calls from Javascript since you have a little more flexibility.
您也可以使用 HTTP 表单上传多个文件,正如 Dave 已经指出的那样,但如果您打算使用 HTTP 和 Javascript 提供的内容之外的内容,我会认真考虑 Flash。甚至有一些预先存在的解决方案,例如MultiPowUpload,它提供了您正在寻找的许多功能。使用 Flash 客户端获取进度信息也比使用来自 Javascript 的 AJAX 调用更容易,因为您有更多的灵活性。
回答by Jay
You may check the Apache Commons FileUpload package. It allows you to upload multiple files, monitor the progress of the upload, and more. You can find more information here:
http://commons.apache.org/fileupload/
http://commons.apache.org/fileupload/using.html
Good luck
您可以查看 Apache Commons FileUpload 包。它允许您上传多个文件、监控上传进度等。您可以在此处找到更多信息:
http: //commons.apache.org/fileupload/
http://commons.apache.org/fileupload/using.html
祝您好运
回答by Gautam
I implemented something very recently in Silverlight.
我最近在 Silverlight 中实现了一些东西。
Basically uses HttpWebRequest to send a chunk of data to a GenericHandler.
基本上使用 HttpWebRequest 将数据块发送到 GenericHandler。
On the first post, 4KB of data is sent. On the 2nd chunk, I send another 4K chunk.
在第一篇文章中,发送了 4KB 的数据。在第二个块上,我发送另一个 4K 块。
When the 2nd chunk is received, I calculate the round trip it took between first and 2nd chunk and so now the 3rd chunk when sent will know to increase speed.
当接收到第二个块时,我计算第一个和第二个块之间的往返行程,因此现在发送的第三个块将知道提高速度。
Using this method I can upload files of ANY size and I can resume.
使用这种方法我可以上传任何大小的文件,我可以继续。
Each post I send along this info:
我随此信息发送的每篇文章:
[PARAMETERS] [FILEDATA]
[参数] [文件数据]
Here, parameters contain the following: [Chunk #] [Filename] [Session ID]
这里,参数包含以下内容: [Chunk #] [Filename] [Session ID]
After each chunk is received, I send a response back to my Silverlight saying how fast it took so that it can now send a larger chunk.
收到每个块后,我将响应发送回我的 Silverlight,说明它现在可以发送更大的块需要多快。
Hard to put my explaination without code but that's basically how I did it.
很难在没有代码的情况下进行解释,但这基本上就是我所做的。
At some point I will put together a quick writeup on how I did this.
在某些时候,我会写一篇关于我如何做到这一点的快速文章。
回答by slf
I've never used it with files of 2GB in size, but the YUI File Uploaderworked pretty well on a previous project. You may also be interested in this jQuery Plugin.
我从未将它用于 2GB 大小的文件,但YUI 文件上传器在以前的项目中运行良好。您可能也对这个jQuery 插件感兴趣。
That said, I still think the Java Applet is the way to go. I think you'll end up with less portability and UI issues than you expect and Drag/Drop works great. For the record, Box.net uses a Java Applet for their multi-file quick uploads.
也就是说,我仍然认为 Java Applet 是要走的路。我认为您最终会遇到比预期更少的可移植性和 UI 问题,并且拖放效果很好。作为记录,Box.net 使用 Java Applet 进行多文件快速上传。
回答by Thierry
what about google gears?
谷歌齿轮怎么样?
回答by Alos
What about these two Jupload http://jupload.sourceforge.net/and jumploader http://jumploader.com/
这两个 Jupload http://jumpload.sourceforge.net/和 jumploader http://jumploader.com/怎么样
Both are java applets but they are also both really easy to use and implement.
两者都是 Java 小程序,但它们也都非常易于使用和实现。