Apache Commons fileUpload实现文件上传(二)
2010年07月30日
[b]示例[/b][b]3[/b]
上传一个文件到指定的目录,并限定文件大小。
demo3.html
File upload
File:
demo3.jsp
上传文件目录
[b]if[/b] (!uploadPath.exists()) {
uploadPath.mkdirs();
}
// 临时文件目录
File tempPathFile = [b]new[/b] File("d:\\temp\\buffer\\");
[b]if[/b] (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
[b]try[/b] {
// Create a factory for disk-based file items
DiskFileItemFactory factory = [b]new[/b] DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);//设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = [b]new[/b] ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List items = upload.parseRequest(request);//得到所有的文件
Iterator i = items.iterator();
[b]while[/b] (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
[b]if[/b] (fileName != [b]null[/b]) {
File fullFile = [b]new[/b] File(fi.getName());
File savedFile = [b]new[/b] File(uploadPath, fullFile
.getName());
fi.write(savedFile);
}
}
out.print("upload succeed");
} [b]catch[/b] (Exception e) {
e.printStackTrace();
}
%>
File upload
[b]示例[/b][b]4[/b]
利用Servlet来实现文件上传。
Upload.java
[b]package[/b] com.zj.sample;
[b]import[/b] java.io.File;
[b]import[/b] java.io.IOException;
[b]import[/b] java.util.Iterator;
[b]import[/b] java.util.List;
[b]import[/b] javax.servlet.ServletException;
[b]import[/b] javax.servlet.http.HttpServlet;
[b]import[/b] javax.servlet.http.HttpServletRequest;
[b]import[/b] javax.servlet.http.HttpServletResponse;
[b]import[/b] org.apache.commons.fileupload.FileItem;
[b]import[/b] org.apache.commons.fileupload.disk.DiskFileItemFactory;
[b]import[/b] org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings("serial")
[b]public[/b] [b]class[/b] Upload [b]extends[/b] HttpServlet {
[b]private[/b] String uploadPath = "D:\\temp"; // 上传文件的目录
[b]private[/b] String tempPath = "d:\\temp\\buffer\\"; // 临时文件目录
File tempPathFile;
@SuppressWarnings("unchecked")
[b]public[/b] [b]void[/b] doPost(HttpServletRequest request, HttpServletResponse response)
[b]throws[/b] IOException, ServletException {
[b]try[/b] {
// Create a factory for disk-based file items
DiskFileItemFactory factory = [b]new[/b] DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = [b]new[/b] ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List items = upload.parseRequest(request);// 得到所有的文件
Iterator i = items.iterator();
[b]while[/b] (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
[b]if[/b] (fileName != [b]null[/b]) {
File fullFile = [b]new[/b] File(fi.getName());
File savedFile = [b]new[/b] File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
} [b]catch[/b] (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] init() [b]throws[/b] ServletException {
File uploadFile = [b]new[/b] File(uploadPath);
[b]if[/b] (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = [b]new[/b] File(tempPath);
[b]if[/b] (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
}
demo4.html
File upload
// action="fileupload"对应web.xml中中的设置.
File:
web.xml
Upload
com.zj.sample.Upload
Upload
/fileupload
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/18331
[b]附件下载:
[/b]
fileUpload工程
2010年07月30日
[b]示例[/b][b]3[/b]
上传一个文件到指定的目录,并限定文件大小。
demo3.html
File upload
File:
demo3.jsp
上传文件目录
[b]if[/b] (!uploadPath.exists()) {
uploadPath.mkdirs();
}
// 临时文件目录
File tempPathFile = [b]new[/b] File("d:\\temp\\buffer\\");
[b]if[/b] (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
[b]try[/b] {
// Create a factory for disk-based file items
DiskFileItemFactory factory = [b]new[/b] DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);//设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = [b]new[/b] ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List items = upload.parseRequest(request);//得到所有的文件
Iterator i = items.iterator();
[b]while[/b] (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
[b]if[/b] (fileName != [b]null[/b]) {
File fullFile = [b]new[/b] File(fi.getName());
File savedFile = [b]new[/b] File(uploadPath, fullFile
.getName());
fi.write(savedFile);
}
}
out.print("upload succeed");
} [b]catch[/b] (Exception e) {
e.printStackTrace();
}
%>
File upload
[b]示例[/b][b]4[/b]
利用Servlet来实现文件上传。
Upload.java
[b]package[/b] com.zj.sample;
[b]import[/b] java.io.File;
[b]import[/b] java.io.IOException;
[b]import[/b] java.util.Iterator;
[b]import[/b] java.util.List;
[b]import[/b] javax.servlet.ServletException;
[b]import[/b] javax.servlet.http.HttpServlet;
[b]import[/b] javax.servlet.http.HttpServletRequest;
[b]import[/b] javax.servlet.http.HttpServletResponse;
[b]import[/b] org.apache.commons.fileupload.FileItem;
[b]import[/b] org.apache.commons.fileupload.disk.DiskFileItemFactory;
[b]import[/b] org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings("serial")
[b]public[/b] [b]class[/b] Upload [b]extends[/b] HttpServlet {
[b]private[/b] String uploadPath = "D:\\temp"; // 上传文件的目录
[b]private[/b] String tempPath = "d:\\temp\\buffer\\"; // 临时文件目录
File tempPathFile;
@SuppressWarnings("unchecked")
[b]public[/b] [b]void[/b] doPost(HttpServletRequest request, HttpServletResponse response)
[b]throws[/b] IOException, ServletException {
[b]try[/b] {
// Create a factory for disk-based file items
DiskFileItemFactory factory = [b]new[/b] DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = [b]new[/b] ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List items = upload.parseRequest(request);// 得到所有的文件
Iterator i = items.iterator();
[b]while[/b] (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
[b]if[/b] (fileName != [b]null[/b]) {
File fullFile = [b]new[/b] File(fi.getName());
File savedFile = [b]new[/b] File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
} [b]catch[/b] (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] init() [b]throws[/b] ServletException {
File uploadFile = [b]new[/b] File(uploadPath);
[b]if[/b] (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = [b]new[/b] File(tempPath);
[b]if[/b] (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
}
demo4.html
File upload
// action="fileupload"对应web.xml中中的设置.
File:
web.xml
Upload
com.zj.sample.Upload
Upload
/fileupload
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/18331
[b]附件下载:
[/b]
fileUpload工程