纯Servlet实现文件上传

项目需求,需要在板子上写一个mini的web项目,由于空间的有限(大概4M)并且项目中有文件上传的功能,所以就选择用servlet实现.

大体步骤就是:  解析request头信息    获取数据流  输入到指定目录 , 但实现的功能有个很严重的缺陷,就是全部将数据流读出来,没有缓存.

导致服务器内存爆满,没有办法读取大的数据流,所以也就放弃了用这种方法实现.具体的方法如下,如有更好的办法,请大家指点一下.
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;


import com.meeboss.meestorage.bean.MeeFile;


/**
 * 文件操作副助类
 * 
 */
public class FileUtil {
/**
* 上传文件到指定目录 并返回文件信息(文件名,大小)

* @param request
* @return
*/
public static MeeFile uploadFileToDest1(HttpServletRequest request,
String dest) {
final int NONE = 0;
final int DATAHEADER = 1;
final int FILEDATA = 2;
final int FIELDDATA = 3;
// 将请求消息的实体送到b变量中
int TotalBytes = request.getContentLength();
byte[] b = new byte[TotalBytes];
String contentType = request.getContentType();// 请求消息类型
String filename = ""; // 文件名
String boundary = ""; // 分界符
String lastboundary = ""; // 结束符
int filesize = 0; // 文件长度
int pos = contentType.indexOf("boundary=");
if (pos != -1) {// 取得分界符和结束符
pos += "boundary=".length();
boundary = "--" + contentType.substring(pos);
lastboundary = boundary + "--";
}
int state = NONE;
// 得到数据输入流reqbuf
try {
DataInputStream in = new DataInputStream(request.getInputStream());
in.readFully(b);
in.close();
String reqContent = new String(b, "UTF-8");
BufferedReader reqbuf = new BufferedReader(new StringReader(
reqContent));
boolean flag = true;
while (flag == true) {
String s = reqbuf.readLine();
if ((s == lastboundary) || (s == null))
break;
switch (state) {
case NONE:
if (s.startsWith(boundary)) {
state = DATAHEADER;
}
break;
case DATAHEADER:
pos = s.indexOf("filename=");
if (pos == -1) {// 将表单域的名字解析出来
pos = s.indexOf("name=");
pos += "name=".length() + 1;
s = s.substring(pos);
int l = s.length();
s = s.substring(0, l - 1);
state = FIELDDATA;
} else {// 将文件名解析出来
String temp = s;
pos = s.indexOf("filename=");
pos += "filename=".length() + 1;
s = s.substring(pos);
int l = s.length();
s = s.substring(0, l - 1);
pos = s.lastIndexOf("\\");
s = s.substring(pos + 1);
filename = s;
// 从字节数组中取出文件数组
pos = byteIndexOf(b, temp, 0);
b = subBytes(b, pos + temp.getBytes().length + 2,
b.length);// 去掉前面的部分
s = reqbuf.readLine();
b = subBytes(b, s.getBytes().length + 4, b.length);
pos = byteIndexOf(b, boundary, 0);
b = subBytes(b, 0, pos - 1);
File f = new File(dest, filename); // 写入文件
DataOutputStream fileout = new DataOutputStream(
new FileOutputStream(f));
fileout.write(b, 0, b.length - 1);
filesize = b.length - 1;
state = FILEDATA;
}
break;
case FIELDDATA:
s = reqbuf.readLine();
state = NONE;
break;
case FILEDATA:
while ((!s.startsWith(boundary))
&& (!s.startsWith(lastboundary))) {
s = reqbuf.readLine();
if (s.startsWith(boundary)) {
state = DATAHEADER;
break;
}
}
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 指定输出类型
MeeFile meeFile = new MeeFile();
meeFile.setName(filename);
meeFile.setLength(String.valueOf(filesize));
return meeFile;
}


/**
* 字节数组中的INDEXOF函数,与STRING类中的INDEXOF类似

* @param b
* @param s
* @param start
* @return
*/
private static int byteIndexOf(byte[] b, String s, int start) {
return byteIndexOf(b, s.getBytes(), start);
}


/**
* 字节数组中的INDEXOF函数,与STRING类中的INDEXOF类似

* @param b
* @param s
* @param start
* @return
*/
private static int byteIndexOf(byte[] b, byte[] s, int start) {
int i;
if (s.length == 0) {
return 0;
}
int max = b.length - s.length;
if (max < 0)
return -1;
if (start > max)
return -1;
if (start < 0)
start = 0;
search: for (i = start; i <= max; i++) {
if (b[i] == s[0]) {
int k = 1;
while (k < s.length) {
if (b[k + i] != s[k]) {
continue search;
}
k++;
}
return i;
}
}
return -1;
}


/**
* 用于从一个字节数组中提取一个字节数组

* @param b
* @param from
* @param end
* @return
*/
private static byte[] subBytes(byte[] b, int from, int end) {
byte[] result = new byte[end - from];
System.arraycopy(b, from, result, 0, end - from);
return result;
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值