我用servlet来做上传,代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=gb2312");
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
String tempfilename = (String) session.getId(); // 会话ID
String path = request.getRealPath("/") + "Accessory/"; // 取物理路经
File f1 = new File(path, tempfilename); //File对象
int n;
try {
InputStream in = request.getInputStream(); // 取得请求输入流
BufferedInputStream my_in = new BufferedInputStream(in);
FileOutputStream fout = new FileOutputStream(f1);
BufferedOutputStream my_out = new BufferedOutputStream(fout);
byte[] b = new byte[10000];
while ((n = my_in.read(b)) != -1) {
my_out.write(b, 0, n);
}
my_out.flush();
my_out.close();
fout.close();
my_in.close();
in.close();
//读取临时文件f1,从中获取上传文件的名字和上传文件的内容。
RandomAccessFile random1 = new RandomAccessFile(f1, "r");
random1.readLine();
String filename = random1.readLine();
byte[] b2 = filename.getBytes("ISO-8859-1");
filename = new String(b2);
int pointer = filename.lastIndexOf('\\');
filename = filename.substring(pointer + 1, filename.length() - 1);
File f2 = new File(path, filename);
RandomAccessFile random2 = new RandomAccessFile(f2, "rw");
random1.seek(0);
for (int i = 1; i <= 4; i++) {
String tempstr = random1.readLine();
}
long startPoint = random1.getFilePointer();
random1.seek(random1.length());
long = random1.getFilePointer();
int j = 0;
long endPoint = 0;
while (( >= 0) && (j <= 5)) {
--;
random1.seek();
n = random1.readByte();
if (n == '\n') {
j++;
endPoint = random1.getFilePointer();
}
}
long length = endPoint - startPoint + 1;
int order = (int) (length / 10000);
int left = (int) (length % 10000);
byte[] c = new byte[10000];
random1.seek(startPoint);
for (int i = 0; i < order; i++) {
random1.read(c);
random2.write(c);
}
random1.read(c, 0, left);
random2.write(c, 0, left);
random1.close();
random2.close();
f1.delete();
out.print("<script language='javascript'>");
out.print("alert('文件上传成功!');");
out.print("window.location='FileUpLoad.jsp';");
out.print("</script>");
} catch (IOException e) {
e.printStackTrace();
out.print("<script language='javascript'>");
out.print("alert('文件上传失败!');");
out.print("window.location='FileUpLoad.jsp';");
out.print("</script>");
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
要怎么限制文件的类型和大小啊??
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=gb2312");
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
String tempfilename = (String) session.getId(); // 会话ID
String path = request.getRealPath("/") + "Accessory/"; // 取物理路经
File f1 = new File(path, tempfilename); //File对象
int n;
try {
InputStream in = request.getInputStream(); // 取得请求输入流
BufferedInputStream my_in = new BufferedInputStream(in);
FileOutputStream fout = new FileOutputStream(f1);
BufferedOutputStream my_out = new BufferedOutputStream(fout);
byte[] b = new byte[10000];
while ((n = my_in.read(b)) != -1) {
my_out.write(b, 0, n);
}
my_out.flush();
my_out.close();
fout.close();
my_in.close();
in.close();
//读取临时文件f1,从中获取上传文件的名字和上传文件的内容。
RandomAccessFile random1 = new RandomAccessFile(f1, "r");
random1.readLine();
String filename = random1.readLine();
byte[] b2 = filename.getBytes("ISO-8859-1");
filename = new String(b2);
int pointer = filename.lastIndexOf('\\');
filename = filename.substring(pointer + 1, filename.length() - 1);
File f2 = new File(path, filename);
RandomAccessFile random2 = new RandomAccessFile(f2, "rw");
random1.seek(0);
for (int i = 1; i <= 4; i++) {
String tempstr = random1.readLine();
}
long startPoint = random1.getFilePointer();
random1.seek(random1.length());
long = random1.getFilePointer();
int j = 0;
long endPoint = 0;
while (( >= 0) && (j <= 5)) {
--;
random1.seek();
n = random1.readByte();
if (n == '\n') {
j++;
endPoint = random1.getFilePointer();
}
}
long length = endPoint - startPoint + 1;
int order = (int) (length / 10000);
int left = (int) (length % 10000);
byte[] c = new byte[10000];
random1.seek(startPoint);
for (int i = 0; i < order; i++) {
random1.read(c);
random2.write(c);
}
random1.read(c, 0, left);
random2.write(c, 0, left);
random1.close();
random2.close();
f1.delete();
out.print("<script language='javascript'>");
out.print("alert('文件上传成功!');");
out.print("window.location='FileUpLoad.jsp';");
out.print("</script>");
} catch (IOException e) {
e.printStackTrace();
out.print("<script language='javascript'>");
out.print("alert('文件上传失败!');");
out.print("window.location='FileUpLoad.jsp';");
out.print("</script>");
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
要怎么限制文件的类型和大小啊??
与你规定的大小对比啊,大于规定的,就抛出异常吧。
int pointer = filename.lastIndexOf('\\');
filename = filename.substring(pointer + 1, filename.length() - 1);filename你不也得到了吗,判断一下后缀不就可以限制你要上传的类型了。