⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mutiparthandler.java

📁 一个完整的代码管理系统的源代码
💻 JAVA
字号:
/*
 * Copyright (c) 2007-2010 Tanming1003 Inc.
 * All rights reserved.
 * 
 * tanming1003<tanming1003@163.com>
 * 
 * 
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * Redistributions of source code must retain the above copyright 
 * notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution. 
 * Neither the name of tanming1003 nor the names of its contributors 
 * may be used to endorse or promote products derived from this 
 * software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package hunnu.edu.cn.product.common.upload;

import hunnu.edu.cn.product.log.LogFactory;
import hunnu.edu.cn.product.log.LogLog;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.ServletInputStream;

/**
 * @author tanming1003@163.com
 * @date Oct 14, 2008
 * @time 9:06:39 PM
 * @project_name Product
 * @package_name hunnu.edu.cn.product.upload
 * @file_name    Context.java
 * @version      1.0
 */
public class MutiPartHandler extends RequestHandler
{	
	private LogLog log=LogFactory.newInstance().getLog(this.getClass());
	
	public MutiPartHandler()
	{
		super();
	}
	
	public MutiPartHandler(Context context)
	{
		super(context);
	}

	public void parseRequest()
	{
	    try
		{
			ServletInputStream in=this.context.getServletRequest().getInputStream();
			String conentType=this.getContentType();
			System.out.println(conentType);
			String s;
			while((s=getString(in))!=null)
			{
				if(s.equals(this.lastboundary))
					break;
				System.out.println(s);
				if(!s.equals(this.boundary))
				{
					String fieldName=getFieldName(s);
					if(s.indexOf("filename")!=-1)
					{
						String fileName=getFileName(s);
						for(int j=0;j<2;j++)
						{
							System.out.println(getString(in));
						}
						FileItemFactory factory=FileItemFactory.newInstance();
						FileItem item=factory.newFileItem("TB_STORAGE");
						item.setFileName(fileName);
						String str;
						ByteArrayOutputStream baos=new ByteArrayOutputStream();
						byte[] bb;
						while((bb=getBytes(in))!=null)
						{
						    str=new String(bb,"gb2312").trim();
						    if(str.equals(this.lastboundary))
						    	break;
						    if(str.equals(this.boundary))
						    	break;
						    baos.write(bb,0,bb.length);
						}
						byte[] b=baos.toByteArray();
						ByteArrayInputStream bais=new ByteArrayInputStream(b,0,b.length);
						item.setInputStream(bais);
						this.addFileItem(item);
					}
					else
					{
						String ss=getString(in);
						System.out.println(ss);
						String value=getString(in);
						this.addField(fieldName,value);
					}
				}
			}
		}
		catch (IOException e)
		{
		   log.debug("IOException", e);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public String getFileName(String name)
	{
		if(name==null)
		   return null;
		String fileName=null;
		int i=name.indexOf("filename=");
		if(i!=-1)
		{
			name=name.substring(i+10,name.length()-1);
			i=name.lastIndexOf("\\");
			if(i!=-1)
				fileName=name.substring(i+1,name.length());
			else
				fileName=name;
		}
		else 
			fileName="";
		return fileName;
	}
	
	public String getString(ServletInputStream in)
	{
		byte[] b=new byte[1024];
		int i;
		try
		{
			if((i=in.readLine(b, 0, b.length))!=-1)
				return new String(b,0,i).trim();
		}
		catch (IOException e)
		{
			log.debug("IOException", e);
		}
		return null;
	}
	
	public byte[] getBytes(ServletInputStream in)
	{
		byte[] b=new byte[4096];
		int i;
		try
		{
			if((i=in.readLine(b, 0, b.length))!=-1)
			{
				byte[] c=new byte[i];
				System.arraycopy(b, 0, c, 0, i);
				return c;
			}
		}
		catch(Exception e)
		{
			return null;
		}
		return null;
	}
	
	public String getFieldName(String string)
	{
		int i=string.indexOf("name=");
		if(i>0)
		{
			if(string.indexOf("filename")!=-1)
			{
				return getFileName(string);
			}
			string=string.substring(i+6,string.length()-1);
			System.out.println(string);
			return string;
		}
		return null;
	}
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -