java檔案上傳

2021-06-15 02:43:45 字數 2506 閱讀 7414

public

static

voidmain(string args)throwsunknownhostexception,ioexception

//    out.println("over");

socket.shutdownoutput();//

傳送過去乙個結束標誌告訴伺服器端結束

//獲取上傳後的資訊

bufferedreaderbufin =newbufferedreader(newinputstreamreader(socket.getinputstream()));

while((line =bufin.readline())!=null) //

關閉資源

socket.close(); }

public

static

voidmain(string args)throwsioexception

bufferedwriterbufw =newbufferedwriter(newfilewriter(file));

//上傳

stringline =null;

while((line =bufin.readline())!=null)

bufw.write(line);

bufw.newline();

bufw.flush();//

必須重新整理}

//上傳成功

s.getoutputstream().write("

上傳成功

".getbytes());

s.close();

ss.close(); }

public

static

voidmain(string args)throwsunknownhostexception,ioexception

//   out.println("over");

socket.shutdownoutput();//

傳送過去乙個結束標誌告訴伺服器端結束

//獲取上傳後的資訊

bufferedreaderbufin =newbufferedreader(newinputstreamreader(socket.getinputstream()));

while((line =bufin.readline())!=null) //

關閉資源

socket.close(); }

public

static

voidmain(string args)throwsioexception //

上傳成功

s.getoutputstream().write("

上傳成功

".getbytes());

s.close();

ss.close(); }

伺服器端的改變

classreadorwriteimplementsrunnable

filefile =newfile(dir,

".***"

);while(file.exists())

fileoutputstreamout =newfileoutputstream(file);

stringline =null;

bytebuf =new

byte[1024];

intlen = 0;

while((len =in.read(buf))!=-1) s

.getoutputstream().write(

"上傳成功

".getbytes());

s.close();

}catch(ioexception e) }

publicreadorwrite(socket s) }

public 

static void main(string args)

}

Java上傳檔案

傳送請求 param url 請求位址 param filepath 檔案在伺服器儲存路徑 這裡是為了自己測試方便而寫,可以將該引數去掉 return throws ioexception private string uploadsource string url,string filepath ...

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 編寫檔案上傳後台 ...