欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

uplaod文件例子 WebFormApacheJSPHTMLServlet 

程序员文章站 2022-06-02 10:07:51
...

要有commons-fileupload-1.1.1.jar和commons-io-1.1.jar包

//post.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>上传页面</title>
</head>
<body>
<form name="Webform" action="upload.jsp" method="post" enctype="multipart/form-data">
输入你的名字:<br/>
<input type="text" name="name" size="15" value="张月"/><br />
图形:<br />
<input type="file" name="myimage"><br/>
文件:<br />
<input type="file" name="myfile"><br /><br />
<input type="submit" name="Submit" value="Submit your files"/>
</form>
</body>
</html>

//upload.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.File"%>
<%@ page import="java.text.*"%>

<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.FileUpload"%>

<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>

<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<%

// 检查输入请求是否为multipart的表单数据。
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
out.print("表单数据格式不是multipart/form-data,或者非法提交");
return;
}

DiskFileItemFactory factory = new DiskFileItemFactory();// Create a factory for disk-based file items
factory.setSizeThreshold(102400);// 设置初始化内存,如果上传的文件超过该大小,将不保存到内存,而且硬盘中(单位:byte)
File fileTemp = new File("c:\\temp");// 建立临时目录
fileTemp.mkdir();
%>
<%--
如果上传的文件超过设置内存的大小,将文件保存至该目录,
待文件上传完毕后,自动删除上传的临时文件;
如果多次上传同一个文件,该文件会保存到服务器的临时目录,而不删除
--%>
<%
factory.setRepository(fileTemp);


ServletFileUpload upload = new ServletFileUpload(factory);// 创建文件上传对象
//DiskFileUpload upload = new DiskFileUpload(); //自己的
upload.setSizeMax(102400);// 设置客户端最大上传,-1为无限大(单位:byte)
//upload.setSizeMax(1024000);
//FileUploadBase.SizeLimitExceededException


List items = null;
try {
items = upload.parseRequest(request);// 分析request,并将保存结果至List里
System.out.println(items);
} catch(SizeLimitExceededException e){
out.print("上传的文件超过系统限制(1MB)");
return;
}


Iterator itr = items.iterator();

while(itr.hasNext()) {// 循环处理表单元素
FileItem item = (FileItem) itr.next();
System.out.println("item is: "+item);
if(item.isFormField()) {// 普通的表单
if("name".equals(item.getFieldName())){
String name = item.getFieldName();
System.out.println(name);
String value = item.getString("gbk");
System.out.println(name+ ":=" + value);
}
}
else {// 上传文件
if(item.getSize()!=0){
String filename = item.getName();// 获取文件的客户端上传路径
int i = filename.lastIndexOf("\\");

filename = filename.substring(i+1);// 获取文件名
System.out.println("文件名:"+filename);
System.out.println("Realpath is:"+getServletContext().getRealPath("/"));
//注意:创建File savedFile时,要在getRealPath("/")的目录下创建一个temp的目录,因为它传了getRealPath("/")+"temp"字符串了.
File savedFile = new File(getServletContext().getRealPath("/") + "temp", filename);
//File savedFile = new File("D:\\xpghost", filename);

System.out.println(item);
item.write(savedFile);
out.println("upload success!!!");
System.out.println("upload success!!!");
}
}
}

%>
</body>
</html>