////servlet 文件
package com.jspservletcookbook;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.*; 还要把这个包放在LIB的目录中去.
import com.oreilly.servlet.multipart.*;
import java.util.*;
public class UploadServlet1 extends HttpServlet {
String web;
public void init(){
web=this.getServletContext().getRealPath("/")+"data";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throw new ServletException("GET method used with"+getClass().getName()+":post method requestird.");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
MultipartRequest mpr=new MultipartRequest(
request,web,100*1024*1024
);
Enumeration enum=mpr.getFileNames();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
for(int i=1;enum.hasMoreElements();i++){
out.println("Then name of upload file"+i+"is: "+mpr.getFilesystemName((String)enum.nextElement())+"<br><br>");
}
}
}
前台页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>LoadFile.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<TABLE border="0">
<form action="UploadServlet1" method="post" enctype="multipart/form-data"> //必须这样写
<td valign="top">
<tr>
<STRONG>
Please choose your document:
</STRONG>
</td>
<td>
<INPUT type="file" name="file1">
</td>
</tr>
<TR>
<TD>
<INPUT type="submit" value="Upload File">
</TD>
</TR>
</form>
</TABLE>
</body>
</html>
==================================================================================================================
upload.jsp
<%@ page language="java" import="java.util.*,java.sql.*,java.io.*,java.util.*,javax.servlet.*,javax.servlet.http.*" pageEncoding="gb2312"%>
<html>
<head>
<title>upFile</title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<body bgcolor=#ffffff>
<%//response.setContentType("gb2312");
int MAX_SIZE=102400*102400;
String rootPath;
DataInputStream in=null;
FileOutputStream fileOut=null;
String remoteAddr=request.getRemoteAddr();
String serverName=request.getServerName();
String realPath=request.getRealPath(serverName);
realPath=realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath=realPath+"\\upload\\";
String contentType=request.getContentType();
try{
if(contentType.indexOf("multipart/form-data")>=0){
in=new DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
if(formDataLength>MAX_SIZE){
out.println("<P>上传的文件字节数不可以超过"+MAX_SIZE+"</P>");
return;
}
byte dataBytes[]=new byte[formDataLength];
int byteRead=0;
int totalBytesRead=0;
while(totalBytesRead<formDataLength){
byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead+=byteRead;
}
String file=new String (dataBytes);
String saveFile=file.substring(file.indexOf("filename=\"")+10);
saveFile=saveFile.substring(0,saveFile.indexOf("\n"));
saveFile=saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
int lastIndex=contentType.lastIndexOf("=");
String boundary=contentType.substring(lastIndex+1,contentType.length());
String fileName=rootPath+saveFile;
int pos;
pos=file.indexOf("filename=\"");
pos=file.indexOf("\n",pos)+1;
pos=file.indexOf("\n",pos)+1;
pos=file.indexOf("\n",pos)+1;
int boundarLocation=file.indexOf(boundary,pos)-4;
int startPos=((file.substring(0,pos)).getBytes()).length;
int endPos=((file.substring(0,boundarLocation)).getBytes()).length;
File checkFile=new File(fileName);
if(checkFile.exists()){
out.println("<p>"+saveFile+"文件已经存在.</p>");
}
File fileDir=new File(rootPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
fileOut=new FileOutputStream(fileName);
fileOut.write(dataBytes,startPos,(endPos-startPos));
fileOut.close();
// String savefile=new String(request.getParameter("FILE1").getBytes("iso-8859-1"),"gb2312");
out.println("<p>http://localhost:7917/company/upload"+saveFile+"文件成功上载.</p>"+formDataLength);
}else{
String conent=request.getContentType();
out.println("<p>上传的数据类型不是是multipart/form-data</p>");
}
}catch(Exception e){
throw new ServletException(e.getMessage());
// out.println("上传文件Error!!!!");
}
%>
<br>
</body>
</html>
前台页面
<html>
<head>
<title>上传文件</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body bgcolor="#ffffff" text="#000000">
<div align="center">
<p><b><font color="#ff6600">上传文件</font></b></p>
<FORM METHOD="POST" ACTION="upload.jsp" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50"><BR>
<INPUT TYPE="SUBMIT" VALUE="上传">
</FORM>
</div>
</body>
</html>