javahttp上傳大文件
① java http協議傳文件
http協議只適合瀏覽器到伺服器的BS模式的文件傳輸,至於PC之間傳文件,可以用TCP/IP或UDP協議
② java 如何實現 http協議傳輸
Java 6 提供了一個輕量級的純 Java Http 伺服器的實現。下面是一個簡單的例子:
public static void main(String[] args) throws Exception{
HttpServerProvider httpServerProvider = HttpServerProvider.provider();
InetSocketAddress addr = new InetSocketAddress(7778);
HttpServer httpServer = httpServerProvider.createHttpServer(addr, 1);
httpServer.createContext("/myapp/", new MyHttpHandler());
httpServer.setExecutor(null);
httpServer.start();
System.out.println("started");
}
static class MyHttpHandler implements HttpHandler{
public void handle(HttpExchange httpExchange) throws IOException {
String response = "Hello world!";
httpExchange.sendResponseHeaders(200, response.length());
OutputStream out = httpExchange.getResponseBody();
out.write(response.getBytes());
out.close();
}
}
然後,在瀏覽器中訪問 http://localhost:7778/myapp/
③ java web 如何獲得文件上傳大小
有一種叫jspsmartupload的包用來簡化文件上傳下載的編寫
裡面可以獲取文件大小
//取得文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//取得文件名
String fileName = myFile.getFileName();
//取得文件大小
int fileSize = myFile.getSize();
這是基於spring架構的上傳文件支持多個文件上傳,拿到file對象後,直接file.size()就可以獲取文件的大小,
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
for (Iterator it = multipartHttpServletRequest.getFileNames(); it.hasNext();) {
String key = (String) it.next();
MultipartFile file = multipartHttpServletRequest.getFile(key);
String originalFilename = file.getOriginalFilename();
long size = file.getSize();//文件大小需要轉換成KB或M
if (StringUtils.isNotBlank(originalFilename)) {
String suffixName = originalFilename.indexOf(".") == -1 ? "" : originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
try {
InputStream inputStream = file.getInputStream();
byte[] ToByteArray = FileCopyUtils.ToByteArray(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
④ Java客戶端通過Http發送POST請求上傳文件
要按http的multi-part上傳的。接收端,再按multi-part解析成文件流
⑤ http 超大文件上傳出現 java.lang.OutOfMemoryError: Java heap space
struts2 的配置文件中有配置上傳文件的默認值大小,不配置的話我記得默認是2M
<constant name="struts.multipart.maxSize" value="104857600" />
⑥ java用戶HTTPURLConnection 上傳文件過程是先把文件寫入流中,然後上傳嗎,可以邊度邊傳嗎
那你就別用這種簡單流去發送,你把你數據分包,用包數據去傳
⑦ http如何實現同時發送文件和報文(用java實現)
你用的servlet還是別的框架?
選POST
選form-data
選body
選File
選文件
Send
// commonsfileupload組件的情況下,servlet接收的數據只能是type=file表單元素類型,那麼獲取type=text類型,就可以使用parseRequest(request)來獲取list,fileitem,判斷isFormField,為true非file類型的。就可以處理了。下面是處理的部分代碼:
DiskFileItemFactoryfactory=newDiskFileItemFactory();factory.setSizeThreshold(1024*1024);
Stringdirtemp="c:";
Filefiledir=newFile(dirtemp+"filetemp");
Stringstr=null;if(!filedir.exists())filedir.mkdir();factory.setRepository(filedir);
ServletFileUploapload=newServletFileUpload(factory);
Listlist=upload.parseRequest(request);for(
inti=0;i<list.size();i++)
{
FileItemitem=(FileItem)list.get(i);
if(item.isFormField()){
System.out.println(item.getString());
}else{
Stringfilename=item.getName();
item.write(newFile(request.getRealPath(dir),filename));
}
}
⑧ HttpUtils怎麼實現大文件的上傳
在struts2中結合HttpClient進行文件上傳 最近遇到了用httpclient進行上傳文件的問題,下面我就和大家簡單的說一下: package com.imps.action; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream;
⑨ 如何使用java實現基於Http協議的大文件傳輸
雖然在JDK的java.net包中已經提供了訪問HTTP協議的基本功能,但是對於大部分應用程序來說,JDK庫本身提供的功能還不夠豐富和靈活。HttpClient是ApacheJakartaCommon下的子項目,用來提供高效的、最新的、功能豐富的支持HTTP協議的客戶端編程工具包,並且它支持HTTP協議最新的版本和建議。以下是簡單的post例子:Stringurl="bbslogin2.php";PostMethodpostMethod=newPostMethod(url);//填入各個表單域的值NameValuePair[]data={newNameValuePair("id","youUserName"),newNameValuePair("passwd","yourPwd")};//將表單的值放入postMethod中postMethod.setRequestBody(data);//執行postMethodintstatusCode=httpClient.executeMethod(postMethod);//HttpClient對於要求接受後繼服務的請求,象POST和PUT等不能自動處理轉發//301或者302if(statusCode==HttpStatus.SC_MOVED_PERMANENTLY||statusCode==HttpStatus.SC_MOVED_TEMPORARILY){//從頭中取出轉向的地址HeaderlocationHeader=postMethod.getResponseHeader("location");Stringlocation=null;if(locationHeader!=null){location=locationHeader.getValue();System.out.println("Thepagewasredirectedto:"+location);}else{System.err.println("Locationfieldvalueisnull.");}return;}詳情見:/developerworks/cn/opensource/os-httpclient/
⑩ httpclient 怎麼實現多文件上傳 c/s java
雖然在JDK的java.net包中已經提供了訪問HTTP協議的基本功能,但是對於大部分應用程序來說,JDK庫本身提供的功能還不夠豐富和靈活。HttpClient是ApacheJakartaCommon下的子項目,用來提供高效的、最新的、功能豐富的支持HTTP協議的客戶端編程工具包,並且它支持HTTP協議最新的版本和建議。以下是簡單的post例子:Stringurl="bbslogin2.php";PostMethodpostMethod=newPostMethod(url);//填入各個表單域的值NameValuePair[]data={newNameValuePair("id","youUserName"),newNameValuePair("passwd","yourPwd")};//將表單的值放入postMethod中postMethod.setRequestBody(data);//執行postMethodintstatusCode=httpClient.executeMethod(postMethod);//HttpClient對於要求接受後繼服務的請求,象POST和PUT等不能自動處理轉發//301或者302if(statusCode==HttpStatus.SC_MOVED_PERMANENTLY||statusCode==HttpStatus.SC_MOVED_TEMPORARILY){//從頭中取出轉向的地址HeaderlocationHeader=postMethod.getResponseHeader("location");Stringlocation=null;if(locationHeader!=null){location=locationHeader.getValue();System.out.println("Thepagewasredirectedto:"+location);}else{System.err.println("Locationfieldvalueisnull.");}return;}詳情見:/developerworks/cn/opensource/os-httpclient/