我可以看到这段代码
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。我做错了什么?
答案 0 :(得分:0)
不,您不必按部件上传文件。在表单中,您可以拥有“file”类型的多个输入字段。
List fileItems = fu.parseRequest(request);
上面的代码会返回您请求中所有“文件”输入字段的列表。因此,如果您有两个文件字段,则会得到两个FileItem
及其内容。下一个声明:
Iterator itr = fileItems.iterator();
用于获取迭代器并迭代刚从请求中提取的FileItem列表。记住,每个FileItem对象都是您上传的文件。