import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
文件上传
步骤:
(1)
*/
public class ProcessUpload extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/**/
System.out.println("I am a serlvet to process upload!!!");
request.setCharacterEncoding( "UTF-8" ); // 从request中取时, 以UTF-8编码解析
// 工厂,
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 获取上传文件存放的 目录 , 无则创建
String path = request.getRealPath( "/upload" );
System.out.println("path : " + path);
new java.io.File( path ).mkdir();
/**
* 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上,
* 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem 格式的
* 然后再将其真正写到 对应目录的硬盘上
*/
diskFileItemFactory.setRepository( new File( path ) );
//设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
diskFileItemFactory.setSizeThreshold( 1024*1024 );
ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
try
{
// 可上传多个文件
@SuppressWarnings("unchecked")
List<FileItem> list = (List<FileItem>) upload.parseRequest( request );
for (FileItem item : list )
{
// 获取 提交表单的 属性名
String name = item.getFieldName();
// 字符串类 属性
if ( item.isFormField() )
{
String value = item.getString();
System.out.println(name + "=" + value);
}
// 二进制类
else
{
// 获取上传文件的名字
String value = item.getName(); // 1,获取路径
int start = value.lastIndexOf( "\\" );// 2,索引到最后一个反斜杠
String filename = value.substring( start+1 );//3, 截取(+1是去掉反斜杠)
System.out.println( name + "=" + value );
File file = null;
do {
// 生成文件名
start = filename.lastIndexOf( "." ); // 索引到最后一个点
filename = filename.substring( 0, start ) // 不含扩展名的文件
+ UUID.randomUUID().toString() // 随机数
+ filename.substring( start ); // 扩展名
file = new File(path, filename);
} while (file.exists());
System.out.println( "filename=" + filename );
// 写到磁盘上去
item.write( file );
System.out.println( "the upload file's size:" + item.getSize() );
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
// -------------------
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
public static void main(String[] args) {
System.out.println( "I am a serlvet to process upload!" );
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
uploads.zip (21个子文件)
uploads
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 459B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 395B
src
ProcessUpload.java 4KB
.project 2KB
WebRoot
css
uploadify.css 2KB
upload
img
uploadify-cancel.png 3KB
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 712B
classes
ProcessUpload.class 4KB
lib
commons-fileupload-1.2.2.jar 58KB
commons-io-2.4.jar 181KB
index.html 5KB
js
jquery.uploadify.min.js 60KB
jquery-1.9.1.js 262KB
uploadify.swf 12KB
.mymetadata 294B
.classpath 529B
.myeclipse
共 21 条
- 1
资源评论
- yangwise1992016-05-03不错的东西
哈哈__哈哈
- 粉丝: 26
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功