0
我可以看到这个代码文件上传和上传的HttpClient项目和部分
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的。我究竟做错了什么?
谢谢你的评论 – user592704 2011-03-14 05:59:41
毫米..这不是一个评论..这是一个答案。你有没有得到你想要的东西? – 2011-03-14 06:01:30
我想用HttpClient和FileUpload上传视频文件(> = 1Gb),但找不到真正有用的代码示例。桌面 - > servlet文件上传。这个任务是否有独立的教程? – user592704 2011-03-14 06:04:06