java客户端仿http传送文件到php服务器
2013-08-28 02:06
621 查看
如果是文件较小可以直接使用传参传递:
java客户端代码:
java端:
php端:
输出结果:
文件名:文件名:fg740p.exe文件长度文件长度:2976536
如果显示413(服务器返回的)
需要修改php.ini和对应服务器的配置文件,将上传文件调到最大就行了,因为我使用的是ngnix所以需要设置的是:
php.info
post_max_size = 10M
upload_max_filesize = 10M
ngnix.conf
找到http{}段,加入如下一句:
client_max_body_size 10m;
弊端,主要是划分界符的问题,如果你传送的字符串或二进制流和|\n|的二进制流一样的话就会产生错误以为数组元素增加了
java客户端代码:
package senssic.demo; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Scanner; public class HttpCon { public static void main(String[] args) { try { File file = new File("d:" + File.separator + "fg740p.exe"); String fname = file.getName(); long flength = file.length(); String date = ""; BufferedReader bfReader = new BufferedReader(new FileReader(file));// 此处如果不是文本文件需要将数据流转换为字符串,post和get只能传递字符串 StringBuilder sBuilder = new StringBuilder(); while ((date = bfReader.readLine()) != null) { sBuilder.append(date); } bfReader.close(); String urlParameters = "fname=" + fname + "&flength=" + flength + "&file=" + sBuilder.toString(); URL url = new URL("http://127.0.0.1/index.php"); URLConnection uConnection = url.openConnection(); HttpURLConnection hConnection = (HttpURLConnection) uConnection; hConnection.setDoInput(true); hConnection.setDoOutput(true); hConnection.setInstanceFollowRedirects(true); hConnection.setRequestMethod("POST");// 可能受限制如果文件比较大,毕竟是通过传参传文件的 hConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); hConnection.setRequestProperty("charset", "utf-8"); hConnection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length)); hConnection.setUseCaches(false); DataOutputStream dStream = new DataOutputStream( hConnection.getOutputStream()); dStream.writeBytes(urlParameters); dStream.flush(); dStream.close(); InputStream iStream = hConnection.getInputStream(); Scanner scanner = new Scanner(iStream); scanner.useDelimiter("\n"); while (scanner.hasNext()) { System.out.println(scanner.next()); } hConnection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }php端代码:
<?php $fp=fopen("d://fg740p.exe","a+"); $str="文件名:".$_POST["fname"]."文件长度:".$_POST["flength"]."文件内容:".$_POST["file"]; fwrite($fp,$_POST["file"]); echo $str; ?>这样缺点比较大,因为post数据限制使用大数据就会不能传递,所以我们使用数据流的方式接收代码如下:
java端:
package senssic.demo; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.SequenceInputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Scanner; public class httpcon2 { public static void main(String[] args) { try { File file = new File("d:" + File.separator + "fg740p.exe"); String fname = file.getName(); long flength = file.length(); InputStream fStream = new FileInputStream(file); String tempstr = "文件名:" + fname + "|\n|文件长度:" + flength + "|\n|";//使用|\n|划分 InputStream iStream = new ByteArrayInputStream((tempstr.getBytes())); SequenceInputStream stream = new SequenceInputStream(iStream, fStream); URL url = new URL("http://127.0.0.1/index.php"); HttpURLConnection hConnection = (HttpURLConnection) url .openConnection(); hConnection.setDoInput(true); hConnection.setDoOutput(true); hConnection.setInstanceFollowRedirects(true); hConnection.setRequestMethod("POST"); hConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); hConnection.setRequestProperty("charset", "utf-8"); hConnection.setUseCaches(false); OutputStream oStream = hConnection.getOutputStream(); int temp = 0; while ((temp = stream.read()) != -1) { oStream.write(temp); } InputStream iconStream = hConnection.getInputStream(); Scanner scanner = new Scanner(iconStream); scanner.useDelimiter("\n"); while (scanner.hasNext()) { System.out.println(scanner.next()); } stream.close(); oStream.flush(); oStream.close(); fStream.close(); iStream.close(); } catch (Exception e) { e.printStackTrace(); } } }
php端:
<?php $fp=fopen("e://fg740p.exe","a+"); $f=file_get_contents("php://input"); $estr=explode("|\n|",$f);//使用|\n|划分 fwrite($fp,$estr[2]); $str="文件名:".$estr[0]."文件长度".$estr[1]; echo $str; ?>
输出结果:
文件名:文件名:fg740p.exe文件长度文件长度:2976536
如果显示413(服务器返回的)
需要修改php.ini和对应服务器的配置文件,将上传文件调到最大就行了,因为我使用的是ngnix所以需要设置的是:
php.info
post_max_size = 10M
upload_max_filesize = 10M
ngnix.conf
找到http{}段,加入如下一句:
client_max_body_size 10m;
弊端,主要是划分界符的问题,如果你传送的字符串或二进制流和|\n|的二进制流一样的话就会产生错误以为数组元素增加了
相关文章推荐
- 在C#客户端用HTTP上传文件到Java服务器
- Asihttprequest上传文件到 appache服务器,脚本语言php(服务器端),客户端iphone oc
- 在C#客户端用HTTP上传文件到Java服务器
- Asihttprequest上传文件到 appache服务器,脚本语言php(服务器端),客户端iphone oc
- 在C#客户端用HTTP上传文件到Java服务器
- Java客户端通过Http发送POST请求上传文件到web服务器
- 在C#客户端用HTTP上传文件到Java服务器
- Java客户端通过Http发送POST请求上传文件到web服务器
- java客户端运用http向服务器批量传输文件
- 迷你开源单文件Java HTTP 服务器---NanoHTTPD.java
- 一个简单地应用--从客户端向服务器发送数据.post,get.httpclientget,httpclinetpost,文件上传.
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- Java实现文件上传服务器和客户端
- PHP-Websockets 上传文件2 优化支持php socket客户端和websocket连接websocket服务器 以守护进程方式运行编码
- Java NIO 服务器与客户端实现文件下载
- Java实现文件上传服务器和客户端
- 【代码笔记】Java深入学习——实现客户端发送文件到服务器的文件传输
- Java客户端通过HttpURLConnection连接Web服务器的Session保存问题
- 服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和android客户端完整代码)
- java实现客户端向服务器发送文件的操作