在文件上传过程中碰到很多问题,首先是搞错了类,刚开始时我用的是PostodMethod,以为一个setrequestbody()方法就可以搞定,结果改过来改过去也没改出来什么名堂,最后改用的是MultipartPostMethod类,呵呵,问题解决了,关键点是MultipartPostMethod类里的addParameter()和addPart()两个方法都要用到,而且要注意顺序。不过马上又出现了新的问题,httpclient不支持中文名的文件上传,晕了。又在这上面浪费了一段时间。解决的途径是。找到httpclient3.0\rc\java\org\apache\commons\httpclient\util目录下的EncodingUtil.java,打开,找到文件里面这个地方: Introducing FileUpload 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 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 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 DiskFileUpload fu = new DiskFileUpload(); List fileItems = fu.parseRequest(request); while(itr.hasNext()) { //Check if not form field so as to only handle the file inputs System.out.println(fNew.getAbsolutePath()); CAUTION With FileUpload 1.0 I found that when the form was submitted using Opera version 7.11, the getName method of the class FileItem returns just the name of the file. However, if the form is submitted using Internet Explorer 5.5, the filename along with its entire path is returned by the same method. This can cause some problems. To run this example, you can use any three files, as the contents of the files are not important. Upon submitting the form using Opera and uploading three random XML files, the output I got on the Tomcat server console was as follows: Content Type =multipart/form-data; boundary=----------rz7ZNYDVpN1To8L73sZ6OE NAME: academy.xml NAME: academyRules.xml NAME: students.xml NAME: D:\temp\academy.xml The browser displayed the following message: The requested resource (D:\javaGizmos\jakarta-tomcat-4.0.1\webapps\HttpServerSideApp\D:\temp\academy.xml (The filename, directory name, or volume label syntax is incorrect)) is not available. This contrasting behavior on different browsers can cause problems. One workaround that I found in an article at is to first create a file reference with whatever is supplied by the getName method and then create a new file reference using the name returned by the earlier file reference. Therefore, you can insert the following code to have your code work with both browsers (I wonder who the guilty party is…blaming Microsoft is always the easy way out) File tempFileRef = new File(fi.getName()); In this section, you uploaded files using a standard HTML form mechanism. However, often a need arises to be able to upload files from within your jsp servlet ejb code, without any browser or form coming into the picture. In the next section, you will look at HttpClient-based file upload. Using HttpClient-Based FileUpload The class org.apache.commons.httpclient.methods.MultipartPostMethod provides the multipart method capability to send multipart-encoded forms, and the package org.apache.commons.httpclient.methods.multipart has the support classes required. Sending a multipart form using HttpClient is quite simple. In the code in Listing 1-3, you send three files to ProcessFileUpload.jsp. Listing 1-3. HttpMultiPartFileUpload.java import org.apache.commons.httpclient.HttpClient; public class HttpMultiPartFileUpload {
|