您的位置:首页 > 理论基础 > 计算机网络

java客户端仿http传送文件到php服务器

2013-08-28 02:06 513 查看
如果是文件较小可以直接使用传参传递:

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|的二进制流一样的话就会产生错误以为数组元素增加了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐