首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

servlet3.0新特性的上传兑现

2012-06-29 
servlet3.0新特性的上传实现?web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnsht

servlet3.0新特性的上传实现

?

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>
*/
@SuppressWarnings("all")
@WebServlet(name="fileUploadServlet",urlPatterns="/fileUploadServlet")
@MultipartConfig(maxRequestSize=222222)//设置文件上传大小
public class FileUploadServlet extends HttpServlet {

/**
*访问
*http://localhost:8080/servlet3.0/
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Part part=req.getPart("file"); //获取页面的name
//System.out.println(part.getName());
System.out.println(System.getProperty("user.dir"));//输出当前的项目存放的路径
String uploadPath=req.getSession().getServletContext().getRealPath("/upload");
System.out.println(uploadPath);//输出上传的文件路径
String value=part.getHeader("content-disposition");//设置头信息
System.out.println(value);
String sub=value.substring(value.lastIndexOf("=")+2,value.length()-1);//截取文件
System.out.println("file size: \t"+part.getSize());//文件的大小
part.write(uploadPath+sub);//写入文件

}
}index.jsp 写道 pageEncoding="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 method="post" enctype="multipart/form-data" action="fileUploadServlet">

file:<input type="file" name="file"/><br/>
<input type="submit" value="submit"/>

</form>
</body>
</html>
?


servlet3.0新特性的上传兑现

源代码?http://download.csdn.net/detail/l_ji_l/4322940

热点排行