日期:2014-05-17 浏览次数:20788 次
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form name="form1" method="post" action="up" enctype="multipart/form-data">
<input type="file" name="select" size=50>
<input type="submit" value="提交">
</form>
</body>
</html>
package sjw.pratice;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class up
*/
@WebServlet("/up")
public class up extends HttpServlet {
private static final long serialVersionUID = 1L;
public up() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletInputStream in=request.getInputStream();
int len=request.getContentLength();
response.setContentType("text/html;utf-8");
PrintWriter out=response.getWriter();
byte[] b=new byte[128];
int i=0;
i=in.readLine(b, 0, 128);
len-=i;
i=in.readLine(b, 0, 128);
len-=i;
String s=new String(b,0,i);
String filename=s.substring(s.lastIndexOf("=")+2,s.lastIndexOf("\""));
if(filename.equals("")==false)
{
String savename=filename.substring(filename.lastIndexOf("\\")+1);
i=in.readLine(b, 0, 128);
len-=i;
i=in.readLine(b, 0, 128);
len-=i;
FileOutputStream fos=new FileOutputStream(request.getRealPath("")+"/"+s