【发布时间】:2011-03-14 05:10:51
【问题描述】:
我可以看到这段代码
DiskFileUpload fu = new DiskFileUpload();
// If file size exceeds, a FileUploadException will be thrown
fu.setSizeMax(1000000);
List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();
while(itr.hasNext()) {
FileItem fi = (FileItem)itr.next();
//Check if not form field so as to only handle the file inputs
//else condition handles the submit button input
if(!fi.isFormField()) {
System.out.println("nNAME: "+fi.getName());
System.out.println("SIZE: "+fi.getSize());
//System.out.println(fi.getOutputStream().toString());
File fNew= new File(application.getRealPath("/"), fi.getName());
System.out.println(fNew.getAbsolutePath());
fi.write(fNew);
}
else {
System.out.println("Field ="+fi.getFieldName());
}
}
我想知道这段代码是什么部分:
List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();
...表示 HttpClient?我应该分部分上传文件还是什么意思?我想用我的桌面应用程序上传视频文件,但我不确定如何组织 HttpClient。 请帮我理解。
客户
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
public class PostFile {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://localhost:8080/uploadtest");
File file = new File("C:\\file.flv");
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "binary/octet-stream");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
}
服务器
public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Content Type ="+request.getContentType());
try {
DiskFileUpload fu = new DiskFileUpload();
// If file size exceeds, a FileUploadException will be thrown
fu.setSizeMax(1000000);
List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();
while (itr.hasNext()) {
FileItem fi = (FileItem) itr.next();
//Check if not form field so as to only handle the file inputs
//else condition handles the submit button input
if (!fi.isFormField()) {
System.out.println("nNAME: " + fi.getName());
System.out.println("SIZE: " + fi.getSize());
//System.out.println(fi.getOutputStream().toString());
File fNew = new File("D:\\uploaded.flv");
System.out.println(fNew.getAbsolutePath());
fi.write(fNew);
}
else {
System.out.println("Field =" + fi.getFieldName());
}
}
}
catch (Exception ex) {
}
}
我想上传文件 >=1Gb。我做错了什么?
【问题讨论】: