资源简介
网上找的一个例子,开始不能用,改后学习完,就共享出来大家学习

代码片段和文件信息
package org.demo.tomcat;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.FileCleanerCleanup;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileCleaningTracker;
/**
*
* @author
* @date 2010-12-16
* @file org.demo.tomcat.UploadServlet.java
*/
public class UploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
this.doPost(req resp);
}
@SuppressWarnings(“unchecked“)
@Override
protected void doPost(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
req.setCharacterEncoding(“utf-8“);
resp.setCharacterEncoding(“utf-8“);
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if (!isMultipart){
System.out.println(“>> This wasn‘t a file upload request!“);
return;
}
PrintWriter out = resp.getWriter();
// create factory and file cleanup tracker
FileCleaningTracker tracker = FileCleanerCleanup.getFileCleaningTracker(getServletContext());
File tmpDir = new File(getbaseDir() + “/upload/temp“);
if (!tmpDir.exists())
tmpDir.mkdirs(); //建立文件夹
DiskFileItemFactory factory = new DiskFileItemFactory(
DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD
tmpDir);
factory.setFileCleaningTracker(tracker);
// save upload file to disk
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(req);
String fileName = null;
File savefile = null;
for (FileItem item : items){
if (!item.isFormField()){
// 确定是文件而不是一个普通的表单字段
fileName = item.getName();
savefile = new File(getbaseDir() + “/upload/“ + fileName);
item.write(savefile);
System.out.println(“>> [save] “ + savefile.getAbsolutePath());
// to client info
out.print(“fileId=“ + savefile.getAbsolutePath());
out.flush();
}
}
} catch (Exception e) {
System.out.println(“>> “ + e.getMessage());
throw new IOException(e.getMessage());
}
}
/**
* Return the WEB-INF directory.
* @return
*/
private String getbaseDir(){
return this.getServletContext().getRealPath(“/WEB-INF“);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-10-19 17:25 SWFTest\
文件 676 2012-10-19 17:25 SWFTest\.classpath
文件 1040 2010-12-17 19:17 SWFTest\.project
目录 0 2012-10-19 17:25 SWFTest\.settings\
文件 454 2010-12-17 19:17 SWFTest\.settings\.jsdtscope
文件 330 2010-12-17 19:17 SWFTest\.settings\org.eclipse.jdt.core.prefs
文件 451 2010-12-17 19:17 SWFTest\.settings\org.eclipse.wst.common.component
文件 229 2010-12-17 19:17 SWFTest\.settings\org.eclipse.wst.common.project.facet.core.xm
文件 49 2010-12-17 19:17 SWFTest\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2010-12-17 19:17 SWFTest\.settings\org.eclipse.wst.jsdt.ui.superType.name
目录 0 2012-10-19 17:25 SWFTest\WebContent\
目录 0 2012-10-19 17:25 SWFTest\WebContent\me
文件 39 2010-12-17 19:17 SWFTest\WebContent\me
目录 0 2012-10-19 17:25 SWFTest\WebContent\WEB-INF\
目录 0 2012-11-05 09:28 SWFTest\WebContent\WEB-INF\classes\
目录 0 2012-11-05 09:28 SWFTest\WebContent\WEB-INF\classes\org\
目录 0 2012-11-05 09:28 SWFTest\WebContent\WEB-INF\classes\org\demo\
目录 0 2012-11-05 09:28 SWFTest\WebContent\WEB-INF\classes\org\demo\tomcat\
文件 3996 2012-11-05 09:28 SWFTest\WebContent\WEB-INF\classes\org\demo\tomcat\UploadServlet.class
目录 0 2012-10-19 17:25 SWFTest\WebContent\WEB-INF\lib\
文件 59590 2010-12-17 19:17 SWFTest\WebContent\WEB-INF\lib\commons-fileupload-1.2.2.jar
文件 159235 2010-12-17 19:17 SWFTest\WebContent\WEB-INF\lib\commons-io-2.0.jar
目录 0 2012-10-19 17:25 SWFTest\WebContent\WEB-INF\upload\
目录 0 2012-11-08 17:57 SWFTest\WebContent\WEB-INF\upload\temp\
文件 851 2010-12-17 19:17 SWFTest\WebContent\WEB-INF\web.xm
目录 0 2012-10-19 17:25 SWFTest\WebContent\flash\
文件 12787 2010-12-17 19:17 SWFTest\WebContent\flash\swfupload.swf
目录 0 2012-10-19 17:25 SWFTest\WebContent\image\
文件 3191 2010-12-17 19:17 SWFTest\WebContent\image\btnBackground.png
文件 1855 2010-12-17 19:17 SWFTest\WebContent\image\btnUpload.png
文件 549 2010-12-17 19:17 SWFTest\WebContent\index.jsp
............此处省略23个文件信息
相关资源
- Java版航空订票系统源码
- 日历时钟java版实现
- java版多人聊天室
- Java版聊天程序(UDP TCP 多线程)
- 棋盘覆盖---JAVA版
- 3.3.6微信支付JAVA版demo
- 微信企业号回调模式Java版
-
java版aspose-words相关jar和license.xm
l - 实验室设备管理系统Java版
- 简易C语言编译器Java源代码
- 《软件设计模式(Java版)》每章习题
- [数据结构Java版第4版][叶核亚][习题解
- windows经典扫雷游戏Java版
- 捕鱼达人java版源码
- 设计模式JAVA版(高清修正版)_刘伟
- maven+ssm 实现java版视频上传预览
- 编译原理课程设计-java版
- 中国象棋java版 保存 悔棋
- 高仿淘宝收货地址,内置全国各个省
- 中国象棋人机对弈Java版源码
- 水果忍者java版
- 微信支付jssdk,java版
- nc文件读写 java版本
- java版语音识别sdk的简单使用
- java版的图书管理系统主要实现添加书
- java版教材管理系统源码.zip
- Java版仿FC坦克大战
- 土豆SDK(Java版)-非官方
- Java版中国象棋项目设计论文和源码
- java版图书管理系统
评论
共有 条评论