上海华普车配件:关于httpclient中MultipartPostMethod类上传文件的一点感受
来源:百度文库 编辑:偶看新闻 时间:2024/04/24 05:14:15
2005-07-09 来源:CSDN 作者:luna8418
在文件上传过程中碰到很多问题,首先是搞错了类,刚开始时我用的是PostodMethod,以为一个setrequestbody()方法就可以搞定,结果改过来改过去也没改出来什么名堂,最后改用的是MultipartPostMethod类,呵呵,问题解决了,关键点是MultipartPostMethod类里的addParameter()和addPart()两个方法都要用到,而且要注意顺序。不过马上又出现了新的问题,httpclient不支持中文名的文件上传,晕了。又在这上面浪费了一段时间。解决的途径是。找到httpclient3.0\rc\java\org\apache\commons\httpclient\util目录下的EncodingUtil.java,打开,找到文件里面这个地方:
public static byte[] getAsciiBytes(final String data) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null"); }
try { return data.getBytes("US-ASCII"); }
catch (UnsupportedEncodingException e) {throw new HttpClientError("HttpClient requires ASCII support"); }
}
看到了没有,returndata.getBytes("US-ASCII");它的编码方式是US-ASCII,问题就出在这里了,把这个取掉,换成"GBK"或者"GB2312"保存以后编译,重新运行程序,goooooooooooood。中文名文件现在可以上传了,呵呵
Introducing FileUpload
The FileUpload component has thecapability of simplifying the handling of files uploaded to a server.Note that the FileUpload component is meant for use on the server side;in other words, it handles where the files are being uploaded to—notthe client side where the files are uploaded from. Uploading files froman HTML form is pretty simple; however, handling these files when theyget to the server is not that simple. If you want to apply any rulesand store these files based on those rules, things get more difficult.
The FileUpload component remedies this situation, and in very fewlines of code you can easily manage the files uploaded and store themin appropriate locations. You will now see an example where you uploadsome files first using a standard HTML form and then using HttpClientcode.
Using HTML File Upload
The commonly used methodology to uploadfiles is to have an HTML form where you define the files you want toupload. A common example of this HTML interface is the Web page youencounter when you want to attach files to an email while using any ofthe popular Web mail services.
In this example, you will create a simple HTML page where youprovide for three files to be uploaded. Listing 1-1 shows the HTML forthis page. Note that the enctype attribute for the form has the valuemultipart/form-data, and the input tag used is of type file. Based onthe value of the action attribute, on form submission, the data is sentto ProcessFileUpload.jsp.
Listing 1-1. UploadFiles.html
Upload Files
You can use a servlet to handle the file upload. I have used JSP tominimize the code you need to write. The task that the JSP has toaccomplish is to pick up the files that are sent as part of the requestand store these files on the server. In the JSP, instead of displayingthe result of the upload in the Web browser, I have chosen to printmessages on the server console so that you can use this same JSP whenit is not invoked through an HTML form but by using HttpClient-basedcode.
Listing 1-2 shows the JSP code. Note the code that checks whetherthe item is a form field. This check is required because the Submitbutton contents are also sent as part of the request, and you want todistinguish between this data and the files that are part of therequest. You have set the maximum file size to 1,000,000 bytes usingthe setSizeMax method.
Listing 1-2. ProcessFileUpload.jsp
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="jsp servlet ejb .util.List"%>
<%@ page import="jsp servlet ejb .util.Iterator"%>
<%@ page import="jsp servlet ejb .io.File"%>
html>
<%
System.out.println("Content Type ="+request.getContentType());
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());
}
}
%>
Upload Successful!!