简体   繁体   English

如何通过Java小程序上传文件?

[英]How to do file uploads via a Java applet?

Specifically how can I: 具体来说,我该如何:

  • Show a button which will let the user browse through his computer and select a file 显示一个按钮,使用户可以浏览他的计算机并选择一个文件
  • Show a progress bar as the files are uploaded 在文件上传时显示进度条
  • And store the files to a location on the server of the website on which the applet is being run 并将文件存储到运行小程序的网站服务器上的某个位置

Any ideas? 有任何想法吗? And yes, I must do this in an applet, and I will make it a trusted/signed applet, have looked into all that. 是的,我必须在一个applet中执行此操作,并且我将使它成为受信任的/已签名的applet,并进行了所有研究。

I've had to do just this, with very large (4Gb+) files. 我必须使用非常大的(4Gb +)文件来执行此操作。 The piece of code at the bottom of this Google Answers post helped me out a LOT: http://answers.google.com/answers/threadview?id=193780 这个Google Answers帖子底部的代码片段帮了我很多忙: http : //answers.google.com/answers/threadview? id= 193780

It showed a way of uploading files chunked into smaller bits, so you can easily use a JProgressBar. 它显示了一种上传文件的方法,将文件分块成小块,因此您可以轻松使用JProgressBar。

There is no need to sign the applet since 6u10. 从6u10开始,就无需对小程序进行签名。 Instead you can use the FileOpenService to read the file through a standard Swing file chooser (technically implementation dependent). 相反,您可以使用FileOpenService通过标准的Swing文件选择器(技术上取决于实现)来读取文件。 Then it is just a matter of sending the file back as a browser would with a multipart MIME HTTP POST . 然后,就像使用多部分MIME HTTP POST一样,将文件发送回浏览器一样。

I would load/stream in the file, convert it to Base64 (or not) and send a POST request (using URLConnection) containing it to a servlet at the other end. 我将加载/流式传输文件,将其转换为Base64(或不转换为Base64),然后将包含它的POST请求(使用URLConnection)发送到另一端的servlet。

From a client side viewpoint its easier than doing a multi-part/form-data file upload and have the Commons FileUpload wait for it on the other end 1 , in my opinion. 从客户端的角度来看,它比进行多部分/表单数据文件上传更容易,并让Commons FileUpload在另一端等待它1 ,在我看来。

1 unless there are libraries available to do just that easily on the client side. 1,除非有可用的库在客户端轻松地做到这一点。

I'm bored. 我很无聊。 I think I'll just copy the links from the last time I answered this question. 我想我只会复制上次回答此问题时的链接。

Signed Applet 签署的小程序

JFileChooser JFileChooser中

JProgressBar 的JProgressBar

JButton 一个JButton

Apache HttpClient 's PostMethod with a MultipartRequestEntity that wraps the JFileChooser's file in a FilePart ). 带有MultipartRequestEntity的 Apache HttpClientPostMethod ,该方法将JFileChooser的文件包装在FilePart中 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM