java http post 同時發送文件流與數據

您好,提問者:
首先表單、文件同時發送那麼肯定是可以的,關於獲取的話很難了,因為發送文件的話form必須設置為:multipart/form-data數據格式,默認為:application/x-www-form-urlencoded表單格式。我們稱之為二進制流和普通數據流。

剛才說了<form的entype要改為multipart/form-data才能進行發送文件,那麼這個時候你表單的另外數據就也會被當成二進制一起發送到服務端。

獲取讀取過來的內容如下:

//拿到用戶傳送過來的位元組流
InputStreamis=request.getInputStream();
byte[]b=newbyte[1024];
intlen=0;
while((len=is.read(b))!=-1){
System.out.println(newString(b,0,len));
}

上面如圖的代碼,我們發現發送過來的表單數據跟文件數據是混亂的,我們根本沒辦法解析(很麻煩),這個時候我們就需要用到第三方輔助(apache 提供的fileupload.jar)來進行獲取。

這個網上有很多代碼的,如果有什麼不明白可以去自行網路,或者追問,我這里只是給你提供的思路,希望理解,謝謝!

㈡ java里http伺服器如何將文件製成一個下載路徑

/**
*文件下載
*/
@RequestMapping("/downloadfile")
(StringresStr,HttpServletResponseresponse){
PrintWriterpw=null;
StringBuffersb=newStringBuffer();

try{
StringfileName="文件名";
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
response.addHeader("Cache-Control","no-cache,no-store,must-revalidate");
response.addHeader("charset","utf-8");
response.addHeader("Pragma","no-cache");
response.setHeader("Content-Disposition","attachment;filename=""+fileName+"";filename*=utf-8''"+fileName);
sb.append(resStr);
pw=response.getWriter();
pw.write(sb.toString());
pw.close();
response.flushBuffer();
}catch(IOExceptione){
logger.info("下載文件出錯");
e.printStackTrace();
if(pw!=null){
pw.close();
}
}finally{
if(pw!=null){
pw.close();
}
}
}

resStr 欄位傳寫入文件里的內容

㈢ java 獲得http下載文件的真實名稱

importjava.net.*;
importjava.io.*;
publicclassURLConnectionDemo{
publicstaticvoidmain(String[]args)throwsException{
URLurl=newURL("http://www.scp.e.cn/pantoschoolzz/BG/Bord/Message/DownloadMessageAttachment.aspx?ID=215");
URLConnectionuc=url.openConnection();
StringfileName=uc.getHeaderField(6);
fileName=URLDecoder.decode(fileName.substring(fileName.indexOf("filename=")+9),"UTF-8");
System.out.println("文件名為:"+fileName);
System.out.println("文件大小:"+(uc.getContentLength()/1024)+"KB");
Stringpath="D:"+File.separator+fileName;
FileOutputStreamos=newFileOutputStream(path);
InputStreamis=uc.getInputStream();
byte[]b=newbyte[1024];
intlen=0;
while((len=is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
System.out.println("下載成功,文件保存在:"+path);
}
}

//輸出內容:

文件名為:090602、09-10(1)校歷.xls

文件大小:42KB

下載成功,文件保存在:D:90602、09-10(1)校歷.xls

㈣ java如何實現收取一個HTTP的請求xml文件.根據文件內容作出響應.返回一個xml文件

接收的完整例子

http://www.tutorialspoint.com/jsp/jsp_file_uploading.htm

讀寫 XML
http://..com/q?ct=17&tn=ikaslist&lm=0&rn=10&pn=0&fr=search&word=java+xml&f=sug&rsp=java+xml

㈤ java,http下載文件

http是流傳輸,一次請求中你是不能將流截斷的,如果想做到你說的只有兩種形式:內
1、就是你使用的方式,多容段讀取(其實就是斷點續傳的原理),多個請求,每個請求請求一部分,如果你覺得效率低可以從兩方面優化
(1)不要用URLConnecion,而使用HttpClient之類的工具包進行請求
(2)使用多線程並發請求,其實就是斷點續傳了,迅雷就是幾個線程一起下嘛
2、只請求一次,請求全部的,將全部流緩存到內存中(byte[]),然後根據你的需要分段截取,寫入文件

㈥ 用JAVA下載HTTP文件時遇到問題

importjava.net.*;
importjava.io.*;
publicclassURLConnectionDemo{
publicstaticvoidmain(String[]args)throwsException{
URLurl=newURL("http://www.scp.e.cn/pantoschoolzz/BG/Bord/Message/DownloadMessageAttachment.aspx?ID=215");
URLConnectionuc=url.openConnection();
StringfileName=uc.getHeaderField(6);
fileName=URLDecoder.decode(fileName.substring(fileName.indexOf("filename=")+9),"UTF-8");
System.out.println("文件名為:"+fileName);
System.out.println("文件大小:"+(uc.getContentLength()/1024)+"KB");
Stringpath="D:"+File.separator+fileName;
FileOutputStreamos=newFileOutputStream(path);
InputStreamis=uc.getInputStream();
byte[]b=newbyte[1024];
intlen=0;
while((len=is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
System.out.println("下載成功,文件保存在:"+path);
}
}

//給你一個下載的例子吧,僅供參考。

㈦ http如何實現同時發送文件和報文(用java實現)

這個算是web項目中的文件上傳功能介面。

  1. java的web項目現在可以使用idea編輯器創建spring boot項目快速構建。(很簡單,具體步驟請網路)

  2. 文件上傳功能也網路吧,一大堆。關鍵詞: spring boot 文件上傳

㈧ java file可以讀取鏈接中的內容嗎 如:ile("https://);裡面是個圖片鏈接地址

http的話就用httpclient。open後,可以返回一個InputStream。這個就是你要讀到文件流。 原理的話,參考你用瀏覽器打開這個鏈接顯示的內容。 這個返回的是一個HTML網頁,需要你解析出裡面的文字(一般來說取body中間的內容就行) 其實對於這種文件一般用FTP來下載的。樓上寫的那個不對,哈哈。 需要的話自己最好去查一下,怎麼用,我有代碼,不過告訴你的話也不太好? URL url = new URL("http://你的地址"); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is,"gb2312")); 下面就是解析這個字元串來,自己來吧

㈨ java http 傳送文件到另一個台伺服器並接收,還有別的參數要傳,請問怎麼實現

socket吧,具體網路好了