[转载]关于httpclient中MultipartPostMethod类上传文件的一点感受

关于httpclient中MultipartPostMethod类上传文件的一点感受

在文件上传过程中碰到很多问题,首先是搞错了类,刚开始时我用的是PostodMethod,以为一个setrequestbody()方法就可以搞定,结果改过来改过去也没改出来什么名堂,最后改用的是MultipartPostMethod类,呵呵,问题解决了,关键点是MultipartPostMethod类里的addParameter()和addPart()两个方法都要用到,而且要注意顺序。不过马上又出现了新的问题,httpclient不支持中文名的文件上传,晕了。又在这上面浪费了一段时间。解决的途径是。找到httpclient3.0 cjavaorgapachecommonshttpclientutil目录下的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"); }
}
看到了没有,return data.getBytes("US-ASCII");它的编码方式是US-ASCII,问题就出在这里了,把这个取掉,换成"GBK"或者"GB2312"保存以后编译,重新运行程序,goooooooooooood。中文名文件现在可以上传了,呵呵

Introducing FileUpload
The FileUpload component has the capability 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—not the client side where the files are uploaded from. Uploading files from an HTML form is pretty simple; however, handling these files when they get to the server is not that simple. If you want to apply any rules and store these files based on those rules, things get more difficult.

The FileUpload component remedies this situation, and in very few lines of code you can easily manage the files uploaded and store them in appropriate locations. You will now see an example where you upload some files first using a standard HTML form and then using HttpClient code.

Using HTML File Upload
The commonly used methodology to upload files is to have an HTML form where you define the files you want to upload. A common example of this HTML interface is the Web page you encounter when you want to attach files to an email while using any of the popular Web mail services.

In this example, you will create a simple HTML page where you provide for three files to be uploaded. Listing 1-1 shows the HTML for this page. Note that the enctype attribute for the form has the value multipart/form-data, and the input tag used is of type file. Based on the value of the action attribute, on form submission, the data is sent to ProcessFileUpload.jsp.

Listing 1-1. UploadFiles.html



File Upload Page

Upload Files
method="post" enctype="multipart/form-data">
File 1:

File 2:

File 3:




You can use a servlet to handle the file upload. I have used JSP to minimize the code you need to write. The task that the JSP has to accomplish is to pick up the files that are sent as part of the request and store these files on the server. In the JSP, instead of displaying the result of the upload in the Web browser, I have chosen to print messages on the server console so that you can use this same JSP when it is not invoked through an HTML form but by using HttpClient-based code.

Listing 1-2 shows the JSP code. Note the code that checks whether the item is a form field. This check is required because the Submit button contents are also sent as part of the request, and you want to distinguish between this data and the files that are part of the request. You have set the maximum file size to 1,000,000 bytes using the setSizeMax method.

Listing 1-2. ProcessFileUpload.jsp






html>



Process File Upload

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(" NAME: "+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!!

转载于:http://blog.itpub.net/374079/viewspace-131152/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值