form 바 이 너 리 흐름 읽 기

2591 단어 자바
package com.utils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
*    
* */
public class FileUtil {

    /**
     *   form       
     * */
       public static Map FileUpload(HttpServletRequest request){
           Map map = new HashMap();
           DiskFileItemFactory factory = new DiskFileItemFactory();
           ServletFileUpload upload = new ServletFileUpload(factory);
           upload.setHeaderEncoding("UTF-8");
          try {
            List items = upload.parseRequest(request);
            Iterator it= items.iterator();
            while(it.hasNext()){
                DiskFileItem item = (DiskFileItem) it.next();
                if(item.isFormField()){
                    //      
                    map.put(item.getFieldName(),item.getString("UTF-8"));
                }else{
                    //     
                    //      
                    map.put("productpictureurl", item.getStoreLocation().getPath());//    
                }
            }
        } catch (FileUploadException | UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           return map;
       }
}
     HTML:

p>

AJAX: function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'http://localhost:8080/cfJAX_RS/rest/file/upload' , type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { alert(returndata); }, error: function (returndata) { alert(returndata); } }); }

좋은 웹페이지 즐겨찾기