用java pyhont通过HTTP协议传输文件流

// 代码网上抄的 忘记链接了 抱歉哈
package
upload; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class uploadtest { public uploadtest() { // TODO Auto-generated constructor stub } public static void main(String[] args) { try { upLoadByCommonPost("http://127.0.0.1/upload_file.php"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void upLoadByCommonPost(String uploadUrl) throws IOException { String end = "\r\n"; String twoHyphens = "--"; String boundary = "******"; URL url = new URL(uploadUrl); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K 应该按照文件大小来定义 // 允许输入输出流 httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); // 使用POST方法 httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream( httpURLConnection.getOutputStream()); dos.writeBytes(twoHyphens + boundary + end); dos.writeBytes("Content-Disposition: form-data; name=\"uploadfile\"; filename=\"1.jpg\";" + end); dos.writeBytes("Content-Type: image/jpeg" + end); dos.writeBytes(end); FileInputStream fis = new FileInputStream("d:\\1.jpg"); byte[] buffer = new byte[1024*100]; // 100k int count = 0; // 读取文件 while ((count = fis.read(buffer)) != -1) { dos.write(buffer, 0, count); } fis.close(); dos.writeBytes(end); dos.writeBytes(twoHyphens + boundary + twoHyphens + end); dos.flush(); InputStream is = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String result; while ((result=br.readLine()) != null){ System.out.println(result); } dos.close(); is.close(); } }

 

再来个python的,用到了poster模块下载地址戳这

1# -*- coding: utf-8 -*-

import urllib2

import urllib

 from poster.encode import multipart_encode
 from poster.streaminghttp import register_openers

      params = {'uploadfile': open(formvalue['file'], "rb")}

2     datagen, headers = multipart_encode(params)

3     request = urllib2.Request(url, datagen, headers)

4     return urllib2.urlopen(request).read().decode("utf_8_sig")

 

服务器代码PHP的

<?php

    var_dump($_FILES);

    if (//($_FILES["type"] == "image/gif")

        //|| ($_FILES["type"] == "image/jpg")

        //|| ($_FILES["type"] == "image/jpeg")

         ($_FILES['uploadfile']["size"] < 200000)){

        if ($_FILES['uploadfile']["error"]>0){

            echo "Error: ".$_FILES['uploadfile']["error"]."<br>";

        }

        else {

            echo "Upload: ". $_FILES['uploadfile']["name"]."<br>";

            echo "Type:".$_FILES['uploadfile']["type"]."<br>";

            echo "Size:".($_FILES['uploadfile']["size"]/1024)."kB<br>";

            echo "Stored in: ".$_FILES["tmp_name"];

        

            if (file_exists("upload/".$_FILES['uploadfile']["name"]))

            {

                echo $_FILES['uploadfile']["name"]." already exists";

            }

            else

            {

                move_uploaded_file($_FILES['uploadfile']["tmp_name"],"upload/".$_FILES['uploadfile']["name"]);

                echo "Stored in: "."upload/".$_FILES['uploadfile']["name"];

            }

        }

    }

    else {

        echo "Invalid file";

    }

        



?>

 

你可能感兴趣的:(http协议)