Java之URL實(shí)現(xiàn)下載

目的

將Java和HTML語(yǔ)言結(jié)合起來(lái)實(shí)現(xiàn)數(shù)據(jù)的下載

具體內(nèi)容

先建一個(gè)HTML文件,可以下載Sublime Text軟件或者新建一個(gè)文本文檔

<!-- 做一個(gè)表單用于提交用戶的數(shù)據(jù)-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登錄</title>
</head>
<body background="1564315747404.jpeg">
   <!-- action 提交內(nèi)容提交到服務(wù)器的哪個(gè)文件中 
method 提交方式 get/post
-->
<form action="login.php" method="get">
<!-- 表單內(nèi)容 -->
<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>

再建一個(gè)PHP文件

<?php  
header('Content-type: text/html; charset=GBk'); 
// 獲取提交的用戶名 get:$_GET post:$_POST
$name=$_POST["user_name"];
$password=$_POST["user_pwd"];
//查詢數(shù)據(jù)庫(kù)
//返回結(jié)果
echo "用戶名:".$name."密碼:".$password;

?>

保存并運(yùn)行
在AndroidStudio里的操作:
1.使用get請(qǐng)求數(shù)據(jù)

public class MyClass {
public static void main(String[] args) throws IOException {
    //使用代碼訪問(wèn)服務(wù)器的數(shù)據(jù)
    //URL
    //1.創(chuàng)建URL
    String path="http://127.0.0.1/login.php?"+"user_name=jackson&user_pwd=123";
    URL url=new URL(path);
    // 獲取連接的對(duì)象
    // URLConnection封裝了socket
    URLConnection connection=url.openConnection();
    //設(shè)置請(qǐng)求方式
    HttpURLConnection httpConnection=(HttpURLConnection)connection;
    httpConnection.setRequestMethod("GET");
    //接收服務(wù)器端的數(shù)據(jù)
    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上傳數(shù)據(jù)

public class MyClass {
public static void main(String[] args) throws IOException {
    post();
}
//使用post上傳數(shù)據(jù)
public static void post() throws IOException {
    //1.創(chuàng)建URL
    URL url=new URL("http://127.0.0.1/login.php");
    //2.獲取connection對(duì)象
    // URLConnection
    // HttpURLConnection 自己需要設(shè)定請(qǐng)求的內(nèi)容
    URLConnection connection=url.openConnection();
    //3.設(shè)置請(qǐng)求方式為post
    ((HttpURLConnection) connection).setRequestMethod("POST");
    //設(shè)置有輸出流 需要上傳
    connection.setDoOutput(true);
    //設(shè)置有輸入流 需要下載
    connection.setDoInput(true);
    //4.準(zhǔn)備上傳的數(shù)據(jù)
    String data="user_name=jackson"+"user_pwd=123";
    //5.開(kāi)始上傳輸出流對(duì)象
    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));
    }
}
//下載數(shù)據(jù) get 不帶參數(shù)
public static void getImage() throws IOException {
    //URL
    URL url=new URL("http://127.0.0.1/1.jpg");
    //獲取與服務(wù)器連接的對(duì)象
    URLConnection connection=url.openConnection();
    //讀取下載的內(nèi)容
    InputStream is=connection.getInputStream();
    //創(chuàng)建文件保存的位置
    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);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 焦初16期 堅(jiān)持分享第79天 郭慧2019-8-23(周 五)晴 (目的、行為與代價(jià)---讓孩子自發(fā)地修正偏差行為...
    牡丹888閱讀 127評(píng)論 0 0
  • 1、推文也稱軟文,就是就是應(yīng)用推廣性質(zhì)的文章,非硬性的,而是在含蓄的文字中,向讀者傳達(dá)了你要推廣的產(chǎn)品、內(nèi)容。 2...
    博望店里有天貓服務(wù)閱讀 359評(píng)論 0 0
  • 昨日又聽(tīng)軍號(hào),再憶軍旗飄。 南昌城頭槍聲響,星星之火燎東方。 轉(zhuǎn)戰(zhàn)兩萬(wàn)五千里,碧血染沃土,人民做主人。 而今戰(zhàn)袍換...
    滌心舟閱讀 625評(píng)論 2 9
  • “城府”很深,這個(gè)詞不知道對(duì)不對(duì)。常聽(tīng)見(jiàn)有人用這個(gè)詞形容一個(gè)人。后來(lái)模糊中漸漸明白,這個(gè)詞可能和喜怒不形于色有點(diǎn)相...
    公子龍羽閱讀 271評(píng)論 0 1
  • 人不要簡(jiǎn)單的理解善與惡,好與壞,真與假,美與丑。 沒(méi)有棱角的善良,不僅不能向世界傳達(dá)你的善意,反而輸送了你的怯意……
    溫商華哥閱讀 111評(píng)論 0 0

友情鏈接更多精彩內(nèi)容