Java上傳檔案

2021-07-04 20:51:34 字數 970 閱讀 4216

/**

* 傳送請求

* * @param url

*            請求位址

* @param filepath

*            檔案在伺服器儲存路徑(這裡是為了自己測試方便而寫,可以將該引數去掉)

* @return

* @throws ioexception

*/private string uploadsource(string url, string filepath)

string filename = file.getname();

/*** 第一部分

*/outputstream out = null;

datainputstream in = null;

bufferedreader reader = null;

try

// 結尾部分

byte foot = ("\r\n--" + boundary + "--\r\n").getbytes("utf-8");// 定義最後資料分隔線

out.write(foot);

out.flush();

/*** 讀取伺服器響應,必須讀取,否則提交不成功

*/stringbuffer sbf = new stringbuffer();

if (con.getresponsecode() == 200)

} catch (exception e)

}return sbf.tostring();

} catch (exception e) finally

} catch (ioexception e)

try

} catch (ioexception e)

try

} catch (ioexception e)

}return null;

}

java檔案上傳

public static voidmain string args throwsunknownhostexception,ioexception out.println over socket.shutdownoutput 傳送過去乙個結束標誌告訴伺服器端結束 獲取上傳後的資訊 bufferedr...

Java大檔案上傳

上傳檔案 如下 public static string uploadsinglefile string path,multipartfile file catch ioexception e else return null 乍一看沒什麼大問題,我在 stream.write bytes 這句加了...

Java檔案上傳實現

1 準備好前台頁面upload.html 表單 action 上傳檔案後台介面 method post enctype multipart form data 檔案輸入框 2 加入相應的jar包 3 準備接收檔案servlet url路徑一定要與上面表單的action保持一致 4 編寫檔案上傳後台 ...