1. 你在Html页面中写下面的语句就可以把文件数据传到servlet中。
2. 在sevelt的代码这样写的,它调用FileLoad这个类来做文件上传。
package fileload.com.lh;
/**
* 此处插入类型描述。
* 创建日期:(2002-11-12 16:10:26)
* @author:无是无非
*/
import Javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
import java.io.*;
import javax.servlet.ServletInputStream;
public class FLServlet extends javax.servlet.http.HttpServlet {
/**
* FLServlet 构造子注解。
*/
public FLServlet() {
super();
}
protected void doGet(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
//上传文件
FileLoad fl = new FileLoad();
fl.prepareForFile(request);
}
}
3. FileLoad类的代码如下。
package fileload.com.lh;
/**
* 此处插入类型描述。
* 创建日期:(2002-11-12 16:14:10)
* @author:无是无非
*/
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
import java.io.*;
import javax.servlet.ServletInputStream;
public class FileLoad {
public ServletInputStream sis = null;
public byte[] b = new byte[4096]; //字节流存放数组
public String sourcefile = ""; //源文件名
private int count = 0; //已传输文件数目
public double size = Double.MAX_VALUE; //限制大小
public String suffix = ""; //文件后缀名
public String objectfilename = ""; //目标文件名
public String objectpath = "e:\\"; //目标文件目录
public boolean sUCcessful = true;
/**
* FileLoad 构造子注解。
*/
public FileLoad() {
super();
}
/**
* 此处插入方法描述。
* 创建日期:(2002-11-14 9:32:16)
* @param response javax.servlet.http.HttpServletResponse
*/
public void prepareForFile(HttpServletRequest request) {
try {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1)
//判定是否从文件中读到内容
{
s = new String(b, 0, a);
if ((k = s.indexOf("filename=")) != -1)
//传过来的每个文件中都有"filename="这段文字
{
s = s.substring(k + 10);
k = s.indexOf("\"");
s = s.substring(0, k); //得到了文件全部名称
sourcefile = s;
k = s.lastIndexOf(".");
suffix = s.substring(k + 1); //得到了文件的扩展名
transferFile();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 此处插入方法描述。
* 创建日期:(2002-11-14 9:16:19)
* @param i int
*/
public void transferFile() {
String x = Long.toString(new java.util.Date().getTime());
try {
objectfilename = x + "." + suffix;
FileOutputStream out = new FileOutputStream(objectpath + objectfilename);
int a = 0;
int k = 0;
long hastransfered = 0; //标示已经传输的字节数
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("Content-Type:")) != -1)
break;
}
sis.readLine(b, 0, b.length);
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((b[0] == 45)
&& (b[1] == 45)
&& (b[2] == 45)
&& (b[3] == 45)
&& (b[4] == 45))
break;
out.write(b, 0, a);
hastransfered += a;
if (hastransfered = 1000) {
System.out.println("文件超大了!!");
successful = false;
break;
}
++count;
}
if (!successful) {
//假如传送文件没有成功就删除掉这个文件
out.close();
sis.close();
String tem = objectpath + objectfilename;
File del = new File(tem);
del.delete();
}
out.close();
} catch (IOException ioe) {
System.out.println("IOException!!");
}
}
}