1.jsp显示页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<font size="4px">commomfile load文件上传示例</font>
<form action="servlet/UploadFile" method="post" enctype="multipart/form-data">
文件: <input type="file" name="file" />
<input type="submit" value="上传" />
</form>
</body>
</html>
2.servlet接受请求并进行处理
package com.servlet.chapter2;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
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 UploadFile extends HttpServlet {
/*
* 用于处理文件上传
* */
public String encoding = "UTF-8";
public String decoding = null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
request.setCharacterEncoding("UTF-8");
ServletInputStream sis = request.getInputStream();
//int length = request.getContentLength();
int index = 0;
String temp = null;
boolean isFirst = true;
String firstLine = null;
int[] iindex = new int[1];
byte[] bytes = new byte[4096];
String filename = null;
while((temp=readLine(bytes, iindex, sis, encoding))!=null){
if(isFirst){
//获取读取文件的开始头部-----------------------------7dd1e2b50256
firstLine = temp;
isFirst = false;
}
index = temp.indexOf("filename=");
//获取上传文件名temp=Content-Disposition: form-data; name="file"; filename="D:\\Program Files\\MyEclipse 8.5\\myeclipse.ini"
if(index!=-1){
String tailString = temp.substring(index+10);
if(tailString!=null){
int ii = tailString.indexOf("\"");
filename = tailString.substring(0,ii);
}
System.out.println(temp);
break;
}
}
filename = getName(filename);
if(filename == null){
filename = "file.out1";
}
String fiepath = "d:/"+filename;
FileOutputStream fos = new FileOutputStream(fiepath);
//定义上传文件的结束标志,与开始头部标记相同-----------------------------7dd1e2b50256-------------------------------7dd1e2b50256
String endFlag = firstLine.substring(0,firstLine.length()-2)+"--"+firstLine.substring(0,firstLine.length()-2);
String contentType = readLine(bytes,iindex,sis,encoding);
if(contentType!=null){
if(contentType.indexOf("Contenty-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((temp = readLine(bytes,iindex,sis,encoding))!=null){
if(endFlag.equals(temp)){
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();
}
//获取上传的文件名
public 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;
}
//读取每一行
public String readLine(byte[] by,int[] index,ServletInputStream sis,String encoding){
try{
index[0] = sis.readLine(by,0, by.length);
if(index[0]<0){
return null;
}
if(encoding == null){
return new String(by,0,by.length);
}
else{
return new String(by,0,by.length);
}
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}