Java之URL实现下载

目的

将Java和HTML语言结合起来实现数据的下载

具体内容

先建一个HTML文件,可以下载Sublime Text软件或者新建一个文本文档

<!-- 做一个表单用于提交用户的数据-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body background="1564315747404.jpeg">
   <!-- action 提交内容提交到服务器的哪个文件中 
method 提交方式 get/post
-->
<form action="login.php" method="get">
<!-- 表单内容 -->
<br>
<br>    
<center>
    用户名:<input type="text" name="user_name">
    <br>
    <br>
密&nbsp&nbsp码:<input type="password" name="user_pwd">
<br>
<br>
  <input type="submit" value="提交">
</center>

</form>
</body>
</html>

再建一个PHP文件

<?php  
header('Content-type: text/html; charset=GBk'); 
// 获取提交的用户名 get:$_GET post:$_POST
$name=$_POST["user_name"];
$password=$_POST["user_pwd"];
//查询数据库
//返回结果
echo "用户名:".$name."密码:".$password;

?>

保存并运行
在AndroidStudio里的操作:
1.使用get请求数据

public class MyClass {
public static void main(String[] args) throws IOException {
    //使用代码访问服务器的数据
    //URL
    //1.创建URL
    String path="http://127.0.0.1/login.php?"+"user_name=jackson&user_pwd=123";
    URL url=new URL(path);
    // 获取连接的对象
    // URLConnection封装了socket
    URLConnection connection=url.openConnection();
    //设置请求方式
    HttpURLConnection httpConnection=(HttpURLConnection)connection;
    httpConnection.setRequestMethod("GET");
    //接收服务器端的数据
    InputStream is=connection.getInputStream();
    byte[] buf=new byte[1024];
    int len;
    while ((len=is.read(buf))!=-1){
        System.out.println(new String(buf,0,len));
      }
   }
}

2.使用post上传数据

public class MyClass {
public static void main(String[] args) throws IOException {
    post();
}
//使用post上传数据
public static void post() throws IOException {
    //1.创建URL
    URL url=new URL("http://127.0.0.1/login.php");
    //2.获取connection对象
    // URLConnection
    // HttpURLConnection 自己需要设定请求的内容
    URLConnection connection=url.openConnection();
    //3.设置请求方式为post
    ((HttpURLConnection) connection).setRequestMethod("POST");
    //设置有输出流 需要上传
    connection.setDoOutput(true);
    //设置有输入流 需要下载
    connection.setDoInput(true);
    //4.准备上传的数据
    String data="user_name=jackson"+"user_pwd=123";
    //5.开始上传输出流对象
    OutputStream os=connection.getOutputStream();
    os.write(data.getBytes());
    os.flush();
    InputStream is=connection.getInputStream();
    byte[] buf=new byte[1024];
    int len;
    while ((len=is.read(buf))!=-1){
        System.out.println(new String(buf,0,len));
    }
}
//下载数据 get 不带参数
public static void getImage() throws IOException {
    //URL
    URL url=new URL("http://127.0.0.1/1.jpg");
    //获取与服务器连接的对象
    URLConnection connection=url.openConnection();
    //读取下载的内容
    InputStream is=connection.getInputStream();
    //创建文件保存的位置
    FileOutputStream fos=new FileOutputStream("C:/Users/Administrator/AndroidStudioProjects/javaday1/src/main/java/day14/1.jpg");
    byte[] buf=new byte[1024];
    int len;
    while ((len=is.read(buf))!=-1){
        fos.write(buf,0,len);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 焦初16期 坚持分享第79天 郭慧2019-8-23(周 五)晴 (目的、行为与代价---让孩子自发地修正偏差行为...
    牡丹888阅读 1,108评论 0 0
  • 1、推文也称软文,就是就是应用推广性质的文章,非硬性的,而是在含蓄的文字中,向读者传达了你要推广的产品、内容。 2...
    博望店里有天猫服务阅读 2,669评论 0 0
  • 昨日又听军号,再忆军旗飘。 南昌城头枪声响,星星之火燎东方。 转战两万五千里,碧血染沃土,人民做主人。 而今战袍换...
    涤心舟阅读 3,693评论 2 9
  • “城府”很深,这个词不知道对不对。常听见有人用这个词形容一个人。后来模糊中渐渐明白,这个词可能和喜怒不形于色有点相...
    公子龙羽阅读 1,679评论 0 1
  • 人不要简单的理解善与恶,好与坏,真与假,美与丑。 没有棱角的善良,不仅不能向世界传达你的善意,反而输送了你的怯意……
    温商华哥阅读 1,031评论 0 0

友情链接更多精彩内容