81,122
社区成员




package lenlib;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
public class CUpLoad {
/*
* 设置:
* <form>标签内增加属性 enctype="multipart/form-data"
*
* 结果:
* request.getContentType获取到内容类型的格式如下:
multipart/form-data; boundary=----WebKitFormBoundary[文件内容的边界编码]
*
* request.getInputStream()转换为字符串后的格式如下:
------WebKitFormBoundary[文件内容的边界编码]
Content-Disposition: form-data; name="[上传表单内file控件的id]"; filename="[上传文件名].[扩展名]"
Content-Type: text/plain
[文件内容]
------WebKitFormBoundary[文件内容的边界编码]--
*
*/
private HttpServletRequest request;
private String sourcefilename; //上传文件原文件名
private String fileext; //上传文件扩展名
private int filesize; //上传文件的大小
private String todirurl; //保存目标文件的路径
private String tofilename; //保存目标文件文件名
public CUpLoad(HttpServletRequest request,String todirurl,String tofilename)
{
this.request=request;
this.todirurl=todirurl;
this.tofilename=tofilename;
}
public String getSourcefilename()
{
return sourcefilename;
}
public String getFileext()
{
return fileext;
}
public int getFilesize()
{
return filesize;
}
public String upload()
{
String strReturn="";
if (request==null) return "未能获取httpSevletRequest对象";
if (todirurl==null || todirurl.trim().equals("")) return "未设置文件上传路径";
if (tofilename==null || tofilename.trim().equals("")) return "未设置文件上传后使用的文件名";
String contentType=request.getContentType();
if (contentType!=null && !contentType.equals("") && contentType.indexOf("multipart/form-data;")>=0)
{
try {
int lastIndex=contentType.lastIndexOf("=");
String boundary=contentType.substring(lastIndex+1,contentType.length());
System.out.println("boundary=\n------------------\n" + boundary + "\n-------------------\n");
DataInputStream in=new DataInputStream(request.getInputStream());
int inLength=request.getContentLength();
byte dataBytes[]=new byte[inLength];
int byteRead=0;
int totalBytesRead=0;
//上传数据保存到数组中
while(totalBytesRead<inLength)
{
byteRead=in.read(dataBytes,totalBytesRead,inLength);
totalBytesRead+=byteRead;
}
String contentAll=new String(dataBytes);
System.out.println("contentAll=\n------------------------\n" + contentAll.substring(0, 1023));
System.out.println(contentAll.substring(contentAll.length()-1024) + "\n-------------------");
int startIndex=contentAll.indexOf("filename=\"") + 10; //文件名的起始位置
int endIndex=contentAll.indexOf("\"",startIndex); //文件名结束的位置
sourcefilename=contentAll.substring(startIndex,endIndex);
if (sourcefilename.lastIndexOf(".")>=0)
{
fileext=sourcefilename.substring(sourcefilename.lastIndexOf("."),sourcefilename.length());
}
else
{
fileext="";
}
startIndex=contentAll.indexOf("\n",contentAll.indexOf("\n",contentAll.indexOf("\n",endIndex)+1)+1)+1; //文件内容起始位置
endIndex=contentAll.lastIndexOf(boundary)-3; //文件内容结束位置
String content=contentAll.substring(startIndex,endIndex);
System.out.println("content=\n------------\n" + content.substring(0,1024) + content.substring(content.length()-1024) + "\n----------------");
filesize=content.getBytes().length;
File fileDir=new File(todirurl);
if (!fileDir.exists())
{
fileDir.mkdirs();
}
int startPos=contentAll.substring(0,startIndex).getBytes().length; //文件流在数据流中的起始位置
int endPos=contentAll.substring(0,endIndex).getBytes().length; //文件流在数据流中的结束位置
FileOutputStream fileOut=new FileOutputStream(todirurl + tofilename + fileext);
fileOut.write(dataBytes,startPos,(endPos-startPos));
// fileOut.write(content.getBytes());
fileOut.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "上传文件流失败!";
}
}
else
{
strReturn="上传<form>标签的类型不正确!";
}
return strReturn;
}
}
<%@ page import="lenlib.*" %>
<%
request.setCharacterEncoding("utf-8");
CUpLoad upload=new CUpLoad(request,"F:/123/","123456789");
System.out.println("stat=" + upload.upload());
System.out.println("fname=" + upload.getSourcefilename());
%>
<form action="upload.jsp method="post" enctype="multipart/form-data">
<font size=2>上传附件:</font>
<input type="file" id="fUpload" name="fUpload">
<input type="submit" value="上传" style="width:80px">
</form>