您的位置:首页 > 编程语言 > Java开发

通过Servlet实现页面上传文件(到硬盘F)

2016-08-31 10:37 323 查看
package servlet4;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileUpLoad extends HttpServlet {

// 编码
private String encoding = "UTF-8";
// 解码
private String decoding = null;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");

ServletInputStream sis = req.getInputStream();

int len = req.getContentLength();
int index = 0;
String tmp = null;
boolean isFirst = true;
String firstLine = null;
int[] iindex = new int[1];
byte[] bytes = new byte[4096];
String filename = null;

while ((tmp = readLine(bytes, iindex, sis, encoding)) != null) {
if (isFirst) {
firstLine = tmp;
isFirst = false;
}
index = tmp.indexOf("filename=");

// 得到要上传文件的文件名
if (index != -1) {
String tailString = tmp.substring(index + 10);
if (tailString != null) {
int ii = tailString.indexOf("\"");
filename = tailString.substring(0, ii);
}
System.out.println(tmp);
break;
}
}
filename = getName(filename);
if (filename == null) {
filename = "file.out1";
}
String filepath = "f:/" + filename;
FileOutputStream fos = new FileOutputStream(filepath);

// 定义文件上传结束标志
String endFlag = firstLine.substring(0, firstLine.length() - 2) + "--"
+ firstLine.substring(firstLine.length() - 2);
String contentType = readLine(bytes, iindex, sis, encoding);
if (contentType != null) {
if (contentType.indexOf("content-type") == -1) {
System.out.println(contentType);
} else {
System.out.println("the head of file"
+ readLine(bytes, iindex, sis, encoding));
}
}
boolean tt = false;
int mark = 0;

byte[] backups = new byte[4096];
while ((tmp = readLine(bytes, iindex, sis, encoding)) != null) {
if (endFlag.equals(tmp)) {
if (mark > 2) {
fos.write(backups, 0, mark - 2);
fos.flush();
}
break;
} else {
if (tt) {
fos.write(backups, 0, mark);
fos.flush();
}
mark = iindex[0];
for (int i = 0; i < iindex[0]; i++) {
backups[i] = bytes[i];
}
tt = true;
}
}
fos.close();
sis.close();
}

// 获取上传的文件名
protected String getName(String name) {
String rtn = null;
if (name != null) {
int index = name.lastIndexOf("/");
if (index != -1) {
rtn = name.substring(index + 1);
} else {
index = name.lastIndexOf("\\");
if (index != -1) {
rtn = name.substring(index + 1);
} else {
rtn = name;
}
}
}
return rtn;
}

// 读取每一行
protected String readLine(byte[] bytes, int[] index,
ServletInputStream sis, String encoding) {
try {
index[0] = sis.readLine(bytes, 0, bytes.length);
if (index[0] < 0)
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
try {
if (encoding == null) {
return new String(bytes, 0, index[0]);
} else {
return new String(bytes, 0, index[0], encoding);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件流 servlet java