zoukankan      html  css  js  c++  java
  • [javaSE] java上传图片给PHP

    java通过http协议上传图片给php文件,对安卓上传图片给php接口的理解

    java文件:

    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    
    public class HttpUpload {
        public static final String API="http://localhost/test.php";
        public static void main(String[] args) throws Exception {
            String imgUrl="E:\11.png";
            String result=uploadImg(imgUrl);
            System.out.println(result);
        }
    
        private static String uploadImg(String imgUrl) throws Exception {
            File imgFile=new File(imgUrl);
            URL url=new URL(API);
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----123456789");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            
            OutputStream os=new DataOutputStream(conn.getOutputStream());
            StringBuilder body=new StringBuilder();
            body.append("------123456789
    ");
            body.append("Content-Disposition: form-data; name='img'; filename='"+imgFile.getName()+"'
    ");
            body.append("Content-Type: image/jpeg
    
    ");
            os.write(body.toString().getBytes());
            
            InputStream is=new FileInputStream(imgFile);
            byte[] b=new byte[1024];
            int len=0;
            while((len=is.read(b))!=-1){
                os.write(b,0,len);
            }
            String end="
    ------123456789--";
            os.write(end.getBytes());
            
            //输出返回结果
            InputStream input=conn.getInputStream();
            byte[] res=new byte[1024];
            int resLen=input.read(res);
            return new String(res,0,resLen);
        }
    }

    PHP文件

    <?php
    class Test{
        public static function main(){
            header("content-type:text/html;charset=utf-8");
            if(!empty($_FILES)){
                $test=new Test();
                $test->uploadImg();
                exit;
            }
        }
        /**
        * 上传图片
        */
        public function uploadImg(){
            $res=move_uploaded_file($_FILES['img']['tmp_name'], './'.$_FILES['img']['name']);
            if($res){
                echo "upload success";
            }else{
                echo "upload error";
            }
        }
    }
    Test::main();
    ?>
    <form enctype="multipart/form-data" action="test.php" method="post">
    <input type="file" name="img" />
    <input type="submit" value="上传" />
    </form>
  • 相关阅读:
    Parallel Programming指南
    使用Autofac IOC组织多项目应用程序
    对SQL Server 2008 R2感兴趣StreamInsight特性
    跨平台团队协作项目源码管理软件Mercurial客户端TortoiseHg
    Windows Server AppFabric正式发布
    Windows NLB搭配IIS的ARR搭建高可用环境
    使用VS2010的Database 项目模板统一管理数据库对象
    Fityk曲线拟合工具
    Windows Server AppFabric Beta 2 for For Vistual Studio 2010已经发布
    Visual Studio 2010快速参考指南里头的Scrum海报
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5721498.html
Copyright © 2011-2022 走看看