หน้าเว็บ

วันอังคารที่ 22 พฤษภาคม พ.ศ. 2555

Java File Upload ด้วย apache common file upload

dependencies
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>13.0.1</version>
            <type>jar</type>
        </dependency>
Client (Java Application)
HttpMultipleFileUpload.java
package com.blogspot.na5cent.client;
import java.io.File;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

public class HttpMultipleFileUpload {
    private static String url = "http://192.168.1.104:8080/MyProject/FileUpload";

    public static void main(String[] args) throws IOException {
        HttpClient client = new HttpClient();
        MultipartPostMethod mPost = new MultipartPostMethod(url);
        client.setConnectionTimeout(30000);

        //get files for upload
        File f1 = new File("c:\\temp\\client\\","java.txt");
        File f2 = new File("c:\\temp\\client\\","html.txt");
        File f3 = new File("c:\\temp\\client\\","php.txt");

        mPost.addParameter(f1.getName(), f1);
        mPost.addParameter(f2.getName(), f2);
        mPost.addParameter(f3.getName(), f3);

        client.executeMethod(mPost);
        mPost.releaseConnection();
    }
}
web application
<form name="form" 
      action="http://192.168.1.104:8080/MyProject/FileUpload" 
      method="post" 
      enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" value="upload"/>
</form>
Server (Java Servlet)
FileUpload.java
package com.blogspot.na5cent.server;
 
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class FileUpload extends HttpServlet {
    private static final File DIRECTORY_UPLOAD = new File("c:\\server\\uploads");

    static{
        if (!DIRECTORY_UPLOAD.exists()) {
            DIRECTORY_UPLOAD.mkdir();
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        
        try {
            List<FileItem> fileUploadItems = upload.parseRequest(request);
            for (FileItem item : fileUploadItems) {
                copyFile(item);
            }
        } catch (FileUploadException ex) {
             System.out.println(ex.getMessage());
        }
    }
    
    public void copyFile(final FileItem item)
        throws IOException {
        Files.copy(new InputSupplier<inputstream>() {
            
            @Override
            public InputStream getInput() throws IOException {
                return item.getInputStream();
            }
        }, new File(DIRECTORY_UPLOAD,item.getName()));
    }
}
web.xml
เพิ่มดังต่อไปนี้ลงไป
<servlet>
    <servlet-name>File Upload</servlet-name>
    <servlet-class>com.blogspot.na5cent.server.FileUpload</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>File Upload</servlet-name>
    <url-pattern>/FileUpload</url-pattern>
</servlet-mapping>
แค่นี้ก็เป็นอันเรียบร้อย


ไม่มีความคิดเห็น:

แสดงความคิดเห็น