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

📄 imgup.java

📁 这是基于jsp的相册管理系统。数据库是mysql
💻 JAVA
字号:
package huitong.util;

import huitong.util.*;
import java.io.*; 
import javax.servlet.ServletInputStream; 
import javax.servlet.http.HttpServletRequest; 

public class ImgUp { 
	public String sourcefile ;//源文件名 
public String objectpath ;//目标文件目录 
String suffix =null;//文件后缀名 
public String objectfilename = null;//目标文件名
public ServletInputStream sis = null;//输入流 
public String description = null;//描述状态
public long size = 1000*10240;//限制大小 
private int count = 0;//已传输文件数目 
private byte[] b = new byte[4096];//字节流存放数组 
private boolean successful = true; 
HttpServletRequest request = null;
public String[]  setSourcefile(HttpServletRequest request) throws java.io.IOException{ 
	this.request = request;
	sis = request.getInputStream(); 
	int a = -1; 
	int fileIndex = -1; 
	boolean typeIndex = false; int t = 0;
	boolean nameIndex = false; int n = 0;
	boolean descIndex =  false; int d = 0;
	String  photoType = null;
	String  photoName = null;
	String  photoDesc = null;
	String photoId = null;
	String s = ""; 
	while((a = sis.readLine(b,0,b.length)) != -1)
	{ 
		
		s = new String(b,0,a); //为字节数组中有效字符串
//		s 等于......filename="C:\Documents and Settings\Administrator.ZHT\×???\XHr.js"......
		fileIndex = getIndexOf(s,"filename=");
		{//代码块,提取参数
			if (photoType==null) 
			{ 
				if (t == 2)photoType = s;if (typeIndex)t++;if (getIndexOf(s,"photoType") > -1){t++;typeIndex = true;}
			}
			if (photoName==null)
			{
				if (n == 2)photoName=s;	if (nameIndex)n++;if (getIndexOf(s,"photoName") > -1){n++;nameIndex = true;}
			}
			if (photoDesc==null)
			{
				if (d == 2)photoDesc =s;if (descIndex)d++;if (getIndexOf(s,"photoDesc") > -1){d++;descIndex = true;}
			}
		}
		
		if( fileIndex != -1)
		{ //获取文件在客户端存放路径及文件类型
			s = s.substring(fileIndex+10);// s 等于 C:\Documents and Settings\Administrator.ZHT\×???\XHr.js"......
			fileIndex = s.indexOf("\""); 
			s = s.substring(0,fileIndex); //s  等于 C:\Documents and Settings\Administrator.ZHT\×???\XHr.js
			// 以上是找客户端上传的文件路径
			sourcefile = s; 
			fileIndex = s.lastIndexOf("."); 
			suffix = s.substring(fileIndex+1); // 找文件的后缀名
			if(canTransfer()) photoId=transferfile(photoType.substring(0,14)); 
		} 
		if(!successful) break; 
	}
	String[] param = {photoType,photoName,photoDesc,suffix,photoId};
	return param;
} 
public int getCount(){ 
return count; 
} 

private int getIndexOf(String source,String str)
{//当匹配时,返回str的首字符在source中的Index
	//当source为空时返回-1
	if (source == null ||source .length() < 1)
		return -1;
	if (str == null)
		return -1;
	if (source.length() < str.length()) return -1;
	return source.indexOf(str);
} 
private String getParam(String source ,String name,char endChar)
{
	int index = getIndexOf(source,name);
	String temp = null;
	if (index > -1)
	{
		temp = source.substring(index);
		index = temp.indexOf(endChar);
	}
	 return temp;
}

public void setObjectpath(String objectpath){ 
this.objectpath = objectpath; 
} 
public String getObjectpath(){ 
return objectpath; 
} 
private boolean canTransfer(){ 
suffix = suffix.toLowerCase(); 
//这个是我用来传图片的,各位可以把后缀名改掉或者不要这个条件 
if( sourcefile.equals("")
	||(!suffix.equals("gif")&&
	   !suffix.equals("jpg")&&
	   !suffix.equals("jpeg")))
{
	description="ERR suffix is wrong";
	return false;
} 
else return true; 
} 
private String transferfile(String albumId)
{ 
	
	String x = "_"+Long.toString(new java.util.Date().getTime()); 
	try
	{ 
		objectfilename = x+"."+suffix;  //在服务器端生成一个文件名
		objectpath += request.getSession().getAttribute("userName")
		                  +"\\"+albumId+"\\";
		System.out.println(objectpath+objectfilename);
		////////////////////////////////*****************************
		// 在这里根据用户名+相册ID(创建时间)+文件号(创建时间) 存放在objectpath变量中 ///////
		//*********************************************************///
		FileOutputStream out = new FileOutputStream(objectpath+objectfilename); 
		int a = 0; 
		int k = 0; 
		long hastransfered = 0;//标示已经传输的字节数 
		String s = ""; 
		while((a = sis.readLine(b,0,b.length)) != -1)
		{ //查找"Content-Type:"字符串
			s = new String(b,0,a); 
			if((k = s.indexOf("Content-Type:")) != -1) 
				break; 
		} 
		if (k != -1)
		{
			String contentType = request.getHeader("Content-Type");
		}
		sis.readLine(b,0,b.length); 
		while((a = sis.readLine(b,0,b.length)) != -1)
		{ 
			s = new String(b,0,a); //为字节数组中有效字符转化为字符串
			if((b[0]==45)&&(b[1]==45)
				&&(b[2]==45)&&(b[3]==45)
				&&(b[4]==45))
				break; //??????
			out.write(b,0,a); 
			hastransfered+=a; 
			if(hastransfered>=size)
			{ 
				description = "ERR The file "+sourcefile+" is too large to transfer. The whole process is interrupted."; 
				successful = false; 
				break; 
			} 
		} 
		if(successful) 
			description = "Right The file "+sourcefile+" has been transfered successfully."; 
		++count; 
		out.close(); 
		if(!successful)
		{ 
			sis.close(); 
			File tmp = new File(objectpath+objectfilename); 
			tmp.delete(); 
		} 
	} 
	catch(IOException ioe)
	{ 
		ioe.printStackTrace();
		description=ioe.toString(); 
	} 
	return x;
}
public ImgUp()
	{ //	可以在构建器里面构建服务器上传目录,也可以在javabean调用的时候自己构建 
		setObjectpath(FinalVar.rootPath); 
	} 
   public static void main(String[] agrs)
   {
	   ImgUp up = new ImgUp();
	   int index = 0;
	   String source = "-----------------------------7d72de1b80220Content-Disposition: form-data; name=\"photoType\"0------------------------";
	   String str = "photoType";
	   String result = up.getParam(source, str,'-');
	   System.out.print(source+"_____"+ str+"  result:  "+result + " \t");	   
   }
} 

⌨️ 快捷键说明

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