急!在线等!请问这段代码,路径哪里设置错了 ?
请问这段代码,路径哪里设置错了 ?
package pageconnection;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
public class UploadFile2 {
private UploadFile2() {
}
private static String getFileType(String fileName) {
int index = -1;
index = fileName.lastIndexOf(".");
if (index>0) {
return fileName.substring(index,fileName.length());
} else {
return "."+fileName.substring(fileName.length()-3,fileName.length());
}
}
public static String[] upload(HttpServletRequest request,String url,String fileName)
throws ServletException,IOException {
return upload(request,url,fileName,false);
}
public static String[] upload(HttpServletRequest request,String url,boolean useAutogenerationName)
throws ServletException,IOException {
return upload(request,url,null,useAutogenerationName);
}
@SuppressWarnings("unchecked")
private static String[] upload(HttpServletRequest request,String url,String fileName,boolean useAutogenerationName)
throws ServletException,IOException {
String name = null;
String value = null;
boolean fileFlag = false;
File tmpFile = null;
String fName = null;
FileOutputStream baos = null;
BufferedOutputStream bos = null;
Hashtable<String, ArrayList<String>> paramHt = new Hashtable<String, ArrayList<String>>();
int BUFSIZE = 1024 * 8;
int rtnPos = 0;
byte[] buffs = new byte[BUFSIZE * 8];
String contentType = request.getContentType();
request.getInputStream() ;
int index = contentType.indexOf("boundary=");
String boundary = "--" + contentType.substring(index + 9);
String endBoundary = boundary + "--";
ServletInputStream sis = null;
try {
sis = request.getInputStream();
} catch (IOException e){
System.out.println("��ȡServlet�ֽ����?");
e.printStackTrace();
throw e;
}
try {
while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
String strBuff = new String(buffs, 0, rtnPos);
if (strBuff.startsWith(boundary)) {
if (name != null && name.trim().length() > 0) {
if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
} else {
Object obj = paramHt.get(name);
ArrayList<String> al = null;
if (obj == null) {
al = new ArrayList<String>();
} else {
ArrayList arrayList = (ArrayList)obj;
al = arrayList;
}
al.add(value);
paramHt.put(name, al);
}
}
name = new String();
value = new String();
fileFlag = false;
rtnPos = sis.readLine(buffs, 0, buffs.length);
if (rtnPos != -1) {
strBuff = new String(buffs, 0, rtnPos);
if (strBuff.toLowerCase().startsWith("content-disposition: form-data; ")) {
int nIndex = strBuff.toLowerCase().indexOf("name=\"");
int nLastIndex = strBuff.toLowerCase().indexOf("\"",nIndex + 6);
name = strBuff.substring(nIndex + 6, nLastIndex);
}
int fIndex = strBuff.toLowerCase().indexOf("filename=\"");
if (fIndex != -1) {
fileFlag = true;
int fLastIndex = strBuff.toLowerCase().indexOf("\"",fIndex + 10);
fName = strBuff.substring(fIndex + 10, fLastIndex);
fIndex = fName.lastIndexOf("\\");
if (fIndex == -1) {
fIndex = fName.lastIndexOf("/");
if (fIndex != -1) {
fName = fName.substring(fIndex + 1);
}
} else {
fName = fName.substring(fIndex + 1);
}
if (fName == null || fName.trim().length() == 0) {
fileFlag = false;
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
continue;
}
}
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
}
} else if (strBuff.startsWith(endBoundary)) {
if (name != null && name.trim().length() > 0) {
if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
} else {
Object obj = paramHt.get(name);
ArrayList<String> al = null;
if (obj == null) {
al = new ArrayList<String>();
} else {
ArrayList<String> arrayList = (ArrayList<String>) obj;
al = arrayList;
}
al.add(value);
paramHt.put(name, al);
}
}
} else {
if (fileFlag) {
if (baos == null && bos == null) {
if (useAutogenerationName == true) {
fName = System.currentTimeMillis()+getFileType(fName);
} else {
if (fileName != null) {
fName = fileName+getFileType(fName);
}
}
tmpFile = new File(url + fName);
baos = new FileOutputStream(tmpFile);
bos = new BufferedOutputStream(baos);
}
bos.write(buffs, 0, rtnPos);
baos.flush();
} else {
value = value + strBuff;
}
}
}
} catch (IOException e) {
System.out.println("д���ļ��쳣�����ļ����ȡ����");
e.printStackTrace();
throw e;
}
try {
if (bos != null ) {
bos.close();
}
} catch (IOException e) {
System.out.println("�ر�д���ֽ����?");
e.printStackTrace();
throw e;
}
if (tmpFile != null) {
//return url + fName;
String message[] = {null,null};
File file = new File(url + fName);
message[0] = fName;
message[1] = getFileSize(file.length());
return message;
} else {
return null;
}
}
protected static String getFileSize(long size) {
StringBuffer sb = new StringBuffer(String.valueOf(size));
int length = sb.length();
if (sb.length()<4) {
sb.append("B");
} else if (sb.length()<7) {
sb = new StringBuffer(sb.substring(0, length-3)+"."+sb.substring(length-3,length-2)+"K");
} else if (sb.length()<10) {
sb= new StringBuffer(sb.substring(0, length-6)+"."+sb.substring(length-6,length-5)+"M");
} else {
sb= new StringBuffer(sb.substring(0, length-9)+"."+sb.substring(length-9,length-8)+"G");
}
return sb.toString();
}
}