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/