`
bluerain_7756
  • 浏览: 2526 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

ENCTYPE="multipart/form-data" 举例应用

 
阅读更多
     表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form- data,才能完整的传递文件数据,进行下面的操作.
enctype=\"multipart/form-data\"是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,即不能用request.getParameter()来获取到各个表单元素的值的
   
  例子如下:
 
 
 
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.Enumeration;
import java.util.Hashtable;


/**
 * java 模拟 ENCTYPE="multipart/form-data"方式
 * 提交FORM
 * */
public class MultipartPOST {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		    URL url = null;
	        HttpURLConnection httpurlconnection = null;
	        try
	        {
	        	/**
	        	 * multipart/form-data  分隔符(boundary)是必须的.
	        	 * ----multipartPostRequestMCUServer 是分隔符,分隔多个文件、表单项
	        	 * */
	            url = new URL("http://10.50.8.225:80/mcu");
	            String boundary = "----multipartPostRequestMCUServer";
	            httpurlconnection = (HttpURLConnection) url.openConnection();
	            httpurlconnection.setDoOutput(true);
	            httpurlconnection.setRequestMethod("POST");
	            httpurlconnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
	            
	            /*put the data from the FORM into a Hashtable*/
	            Hashtable<String,String> hashtable = new Hashtable<String,String>();
	            hashtable.put("peername", "10");
	            hashtable.put("operation", "login");
	            String boundaryMessage = getBoundaryMessage(boundary,hashtable,"","","");
	            String endBoundary = "\r\n--" + boundary + "--\r\n";
	            System.out.println( boundaryMessage);
	            
	            OutputStream dout = httpurlconnection.getOutputStream();
	            dout.write(boundaryMessage.getBytes());
	            dout.write(endBoundary.getBytes());
	            dout.flush();
	            dout.close();
	            
	            int code = httpurlconnection.getResponseCode();
	            System.out.println("-------code   " + code);

	            InputStream is = httpurlconnection.getInputStream();
	            
               try {
	                byte[] buffer = new byte[1024];  
	                @SuppressWarnings("unused")
					int len = -1;  
	                while ((len = is.read(buffer)) != -1) {  
	                	System.out.println(new String(buffer));
	                }
                } finally {
                	is.close();
                }  

	        }catch (Exception e)
	        {
	            e.printStackTrace();
	        }
	        finally
	        {   
	            if (httpurlconnection != null)
	                httpurlconnection.disconnect();

	        }
		
	}
	
	
	@SuppressWarnings("unchecked")
	static String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType)
	{
		StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n");
		Enumeration keys = params.keys();
		while(keys.hasMoreElements())
		{
			String key = (String)keys.nextElement();
			String value = (String)params.get(key);
			res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n")    
				.append("\r\n").append(value).append("\r\n")
				.append("--").append(boundary).append("\r\n");
		}
		if(fileField != null && !"".equals(fileField) && fileName != null && !"".equals(fileName) && fileType!=null&&!"".equals(fileType) ){
			res.append("Content-Disposition: form-data; name=\"").append(fileField).append("\"; filename=\"").append(fileName).append("\"\r\n") 
			.append("Content-Type: ").append(fileType).append("\r\n\r\n");
		}
		return res.toString();
	}

}

  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics