xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
package com.upload;
import java.io.*;
import javax.servlet.http.httpservletrequest;
import javax.servlet.servletinputstream;
import javax.servlet.servletexception;
public class upload{
private static string newline = "\n";
private string uploaddirectory = ".";
private string contenttype = "";
private string characterencoding = "";
private string getfilename(string s){
int i = s.lastindexof("\\");
if(i < 0 // i >= s.length() - 1){
i = s.lastindexof("/");
if(i < 0 // i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}
public void setuploaddirectory(string s){
uploaddirectory = s;
}
public void setcontenttype(string s){
contenttype = s;
int j;
if((j = contenttype.indexof("boundary=")) != -1){
contenttype = contenttype.substring(j + 9);
contenttype = "--" + contenttype;
}
}
public void setcharacterencoding(string s){
characterencoding = s;
}
public void uploadfile( httpservletrequest req) throws servletexception, ioexception{
setcharacterencoding(req.getcharacterencoding());
setcontenttype(req.getcontenttype());
uploadfile(req.getinputstream());
}
public void uploadfile( servletinputstream servletinputstream) throws servletexception, ioexception{
string s5 = null;
string filename = null;
byte linebyte[] = new byte[4096];
byte outlinebyte[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
string line;
//得到文件名
while((line = readline(linebyte, ai, servletinputstream, characterencoding)) != null){
int i = line.indexof("filename=");
if(i >= 0){
line = line.substring(i + 10);
if((i = line.indexof("\"")) > 0)
line = line.substring(0, i);
break;
}
}
filename = line;
if(filename != null && !filename.equals("\"")){
filename = getfilename(filename);
string scontenttype = readline(linebyte, ai, servletinputstream, characterencoding);
if(scontenttype.indexof("content-type") >= 0)
readline(linebyte, ai, servletinputstream, characterencoding);
//file(string parent, string child)
//creates a new file instance from a parent pathname string
//and a child pathname string.
file file = new file(uploaddirectory, filename);
//fileoutputstream(file file)
//creates a file output stream to write to the file represented
//by the specified file object.
fileoutputstream fileoutputstream = new fileoutputstream(file);
while((scontenttype = readline(linebyte, ai, servletinputstream, characterencoding)) != null){
if(scontenttype.indexof(contenttype) == 0 && linebyte[0] == 45)
break;
if(s5 != null){
//write(byte[] b, int off, int len)
//writes len bytes from the specified byte array starting
//at offset off to this file output stream.
fileoutputstream.write(outlinebyte, 0, ai1[0]);
fileoutputstream.flush();
}
s5 = readline(outlinebyte, ai1, servletinputstream, characterencoding);
if(s5 == null // s5.indexof(contenttype) == 0 && outlinebyte[0] == 45)
break;
fileoutputstream.write(linebyte, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s5 != null && outlinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(outlinebyte, 0, ai1[0] - newline.length() * byte0);
if(scontenttype != null && linebyte[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(linebyte, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
}
private string readline(byte linebyte[], int ai[],
servletinputstream servletinputstream,
string characterencoding){
try{
//readline(byte[] buffer, int offset, int length)
//reads a line from the post data.
ai[0] = servletinputstream.readline(linebyte, 0, linebyte.length);
if(ai[0] == -1)
return null;
}catch(ioexception _ex){
return null;
}
try{
if(characterencoding == null){
//用缺省的编码方式把给定的byte数组转换为字符串
//string(byte[] bytes, int offset, int length)
return new string(linebyte, 0, ai[0]);
}else{
//用给定的编码方式把给定的byte数组转换为字符串
//string(byte[] bytes, int offset, int length, string enc)
return new string(linebyte, 0, ai[0], characterencoding);
}
}catch(exception _ex){
return null;
}
}
/*
public int readline(byte[] buffer,
int offset,
int length) throws java.io.ioexception
从post来的数据中读一行
参数:
buffer - buffer to hold the line data
offset - offset into the buffer to start
length - maximum number of bytes to read.
returns:
number of bytes read or -1 on the end of line.
*/
}
upload.jsp
<%@page import="com.upload.upload"%>
<%
string dir = "/home/tonywan/upload";
upload upload = new upload();
upload.setuploaddirectory(dir);
upload.uploadfile(request);
out.print("<center><font color=red>成功上载文件至" + dir + "</font></center>");
%>
upload.html
<center>file upload test</center>
<br>
<table><tr>
<form method="post" enctype="multipart/form-data" action="upload.jsp">
<td> <input type=file size=20 name="fname"> </td>
<td> <input type=submit value=upload> </td> </form>
</tr></table>
不知道大家看地懂吗?反正使用起来很简单的,只要设置一下上传路径就可以了。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器