用于上传文件的 Java 小程序
我正在寻找一个 Java 小程序来从客户端计算机读取文件并创建 PHP 服务器上传的 POST 请求。
服务器上的 PHP 脚本应该像 FORM 提交中的正常文件上传一样接收该文件。 我正在使用以下代码。文件内容传递给 PHP 脚本 但它们没有正确转换为图像。
//uploadURL will be a url of PHP script like
// http://www.example.com/uploadfile.php
URL url = new URL(uploadURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
InputStream is = new FileInputStream("C://img.jpg");
OutputStream os = con.getOutputStream();
byte[] b1 = new byte[10000000];
int n;
while((n = is.read(b1)) != -1) {
os.write("hello" , 0, 5);
test += b1;
}
con.connect();
I am looking for a Java applet to read a file from client machine and creat a POST request for PHP server uploading.
PHP script on server should receive the file as normal file upload in FORM submit.
I am using the following code. The file contents are passed to PHP script
but they are not correctly converted to an image.
//uploadURL will be a url of PHP script like
// http://www.example.com/uploadfile.php
URL url = new URL(uploadURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
InputStream is = new FileInputStream("C://img.jpg");
OutputStream os = con.getOutputStream();
byte[] b1 = new byte[10000000];
int n;
while((n = is.read(b1)) != -1) {
os.write("hello" , 0, 5);
test += b1;
}
con.connect();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些代码可能会对您有所帮助,它来自我的一个旧项目,删除了一堆不相关的内容,请考虑它的价值。基本上,我认为你问题中的代码缺少 HTTP 协议所需的一些部分
Here is some code that might help you it's from one of my old projects with a bunch of unrelated stuff removed, take it for what it's worth. Basically, I think the code in your question is missing some parts that the HTTP protocol requires
我建议您查看Gallery Remote。这是一个用于将照片上传到 PHP 后端的开源项目。它的功能比您可能需要的功能更全面,但是您应该能够相当轻松地根据您的需要修改代码。
您还可以查看 JUpload。它的功能并不齐全,但它是开源的并且能够胜任这项任务。
I'd suggest you take a look at Gallery Remote. This is an open source project for uploading photos to a PHP backend. It's a bit more full featured than what you may need, but you should be able to modify the code to your needs fairly easily.
You could also look at JUpload. It's not as full featured, but it is open source and capable of the task.