<form action="upjsp" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit" value="确定">
</form>
上传页面upjsp
<%@page import="javaioFileWriter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="javaio"
pageEncoding="UTF-8"%>
<%
/
协议头四行内容
45 -----------------------------7de231211204c4
80 Content-Disposition: form-data; name="file"; filename="xxtxt"
26 Content-Type: text/plain
2
标记文件结尾
-----------------------------7de231211204c4--
/
ServletInputStream sin = requestgetInputStream();
byte[] buffer = new byte[1024 8];
int length = 0, row = 0;
String mark = "";
String filename = "";
while ((length = sinreadLine(buffer, 0, bufferlength)) > 0) {
outprintln(length + " " + new String(buffer, 0, length, "UTF-8") + "<br>");
String s = new String(buffer, 0, length, "UTF-8");
if (row == 0)
mark = strim();
else if (sindexOf("filename=") > 0) {
int end = slastIndexOf("\"");
int start = ssubstring(0, end)lastIndexOf("\"");
filename = ssubstring(start + 1, end);
} else if (""equals(strim()))
break;
row ++;
}
outprintln("filename: " + filename + "<br>");
filename = requestgetRealPath("/") + "/" + filename;
FileOutputStream fout = new FileOutputStream(filename);
while ((length = sinreadLine(buffer, 0, bufferlength)) > 0) {
String s = new String(buffer, 0, length);
if (sstartsWith(mark))
break;
foutwrite(buffer, 0, length);
}
foutflush();
foutclose();
File f = new File(filename);
outprintln(fexists());
outprintln(fgetAbsolutePath());
%>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)