关于httpclient中MultipartPostMethod类上传文件…

2008-02-23 09:37:34来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

在文件上传过程中碰到很多问题,首先是搞错了类,刚开始时我用的是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"); }
}
看到了没有,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
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Windows-1252"/>
<TITLE>File Upload Page</TITLE>
</HEAD>
<BODY>Upload Files
<FORM name="filesForm" action="ProcessFileUpload.jsp"
method="post" enctype="multipart/form-data">
File 1:<input type="file" name="file1"/><br/>
File 2:<input type="file" name="file2"/><br/>
File 3:<input type="file" name="file3"/><br/>
<input type="submit" name="Submit" value="Upload Files"/>
</FORM>
</BODY>
</HTML>

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
<%@ 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Process File Upload</title>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java.security.Guard翻译

下一篇:Java学习笔记(一)