package com.test.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
//
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
String path= request.getRealPath("/upload");
factory.setRepository(new File(path));
factory.setSizeThreshold(1024*1024);
ServletFileUpload upload=new ServletFileUpload(factory);
try
{
List<FileItem> list = upload.parseRequest(request);
for(FileItem item : list)
{
if(item.isFormField())
{
String name=item.getFieldName();
String value=item.getString("gbk");
System.out.println(name);
request.setAttribute(name, value);
}
else
{
String name=item.getFieldName();
String value=item.getName();
int start=value.lastIndexOf("\\");
String fileName=value.substring(start+1);
request.setAttribute(name, fileName);
OutputStream os=new FileOutputStream(new File(path,fileName));
InputStream is =item.getInputStream();
byte[] buffer=new byte[400];
int length=0;
while((length=is.read(buffer))>0){
os.write(buffer, 0, length);
}
os.close();
is.close();
}
}
}
catch(Exception ex){
ex.printStackTrace();
}
request.getRequestDispatcher("upload/result2.jsp").forward(request,response);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result2.jsp' starting page</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>
username:${requestScope.username}<br>
password:${requestScope.password}<br>
file1:${requestScope.file1}<br>
file2:${requestScope.file2}
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'upload2.jsp' starting page</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>
<form action="/struts2/UploadServlet" method="post" enctype="multipart/form-data">
username:<input type="text" name="username" ><br>
password:<input type="password" name="password"><br>
file1:<input type="file" name="file1">
file2:<input type="file" name="file2">
<input type="submit" value=" submit ">
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.test.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
</web-app>
posted on 2009-03-18 22:07
asiawang 阅读(439)
评论(1) 编辑 收藏