?? imgup.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 + -