servlet3.0 上传文件

异步上传

 

package com.beusoft.wc.api;

import com.beusoft.wc.IWordCounter;
import com.beusoft.wc.Input;
import com.beusoft.wc.Result;
import com.beusoft.wc.WordCounterRegister;
import com.google.common.io.Files;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;

 
@WebServlet(urlPatterns = "/upload-wc", loadOnStartup = 2, asyncSupported = true)
@MultipartConfig()
public class UploadWordCountServlet extends HttpServlet {

   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.isAsyncSupported()) {
            AsyncContext actx = request.startAsync();
            actx.start(new Task(actx));  
        } else {
            throw new RuntimeException("Do not support asynchronous request");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h3>Hello</h3>");
        
        out.println("</body>");
        out.println("</html>");
        out.flush();
    }

    class WordCountTask implements Runnable {
        private AsyncContext context;
        private HttpServletRequest request;
        private HttpServletResponse response;

        public WordCountTask(AsyncContext context) {
            this.context = context;
            this.request = (HttpServletRequest) context.getRequest();
            this.response = (HttpServletResponse) context.getResponse();
        }

        @Override
        public void run() {
            try {
                response.setContentType("application/json;charset=UTF-8");
//                Result all=new Result();
                for (Part part : request.getParts()) {
                    ....
 
                    
                    response.getWriter().write("....");
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                context.complete();
            }
        }
    }

    //part.getSubmittedFileName() 在servlet 3.1中才支持,但是tomcat8才支持3.1规范
    public static String getPartSubmittedFileName(Part part) {
        String cd = part.getHeader("content-disposition");
        if (cd != null) {
            String[] cds = cd.split(";");
            for (String kv : cds) {
                if (kv.trim().startsWith("filename=")) {
                    return kv.split("=")[1].replace("\"", "");
                }
            }
        }
        return "";
    }
}

 

你可能感兴趣的:(servlet3.0)