资源简介
图片上传功能几乎是每一个项目都必备的,而没遇到一次都要重新写一篇,就陷入了重复造轮子的圈子中,所以今天把这个上传图片的小demo上传到网上,供大家以后遇到的时候直接使用,提供方便,避免做重复的事情。
代码片段和文件信息
package com.upload.demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
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.ServletFileUpload;
import com.lidroid.xutils.http.RequestParams;
public class UploadServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
doPost(req resp);
};
@Override
protected void doPost(HttpServletRequest request
HttpServletResponse response) throws ServletException IOException {
//服务器端接收请求被后台拦截到这里
exeUploadedInfos(request response);
};
/**
* 定义接口
*/
public interface UploadStatusListener {
public void status(boolean status Map params
List files HttpServletRequest request);
}
UploadStatusListener mUploadStatusListener = null;
// 注册监听器,如果这里没有使用Vector而是使用ArrayList那么要注意同步问题
public void addStatusListener(UploadStatusListener listener) {
mUploadStatusListener = listener;
}
/**
* @function:回调方法,执行图片上传,根据成功或失败的状态完成其他的操作
* @param request
* @param response
*/
private void exeUploadedInfos(HttpServletRequest request
HttpServletResponse response) {
UploadServlet up = new UploadServlet();
up.addStatusListener(new UploadStatusListener() {
public void status(boolean status Map params
List files HttpServletRequest request) {
if (status) {//成功
//Map中取值
String memberId = params.get(“memberId“);
//List中取值
String fileUrl = files.get(0);
System.out.println(“===================================成功“);
} else {
//失败
System.out.println(“========================================失败“);
}
}
});
up.upload(request response);
}
/**
* @function:具体上传图片的功能在这里完成,
* 然后调用接口回调方法mUploadStatusListener.status(flag params filenames request);跳转到回调函数中
* @param request
* @param response
*/
public void upload(HttpServletRequest request HttpServletResponse response) {
boolean flag = false;
Map params = new HashMap();
List filenames = new ArrayList();
try {
System.out.println(“开始文件上传****************“);
request.setCharacterEncoding(“utf-8“); // 设置编码
// 设置上传文件最大为 3M
final long MAX_SIZE = 3 * 1024 * 1024;
// 1.创建一个解析器工厂
DiskFile
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1168 2015-12-01 09:49 UploadDemo\.classpath
文件 1043 2015-11-23 12:24 UploadDemo\.project
文件 503 2015-11-23 12:24 UploadDemo\.settings\.jsdtscope
文件 364 2015-11-23 12:24 UploadDemo\.settings\org.eclipse.jdt.core.prefs
文件 484 2015-11-23 12:24 UploadDemo\.settings\org.eclipse.wst.common.component
文件 345 2015-11-23 12:24 UploadDemo\.settings\org.eclipse.wst.common.project.facet.core.xm
文件 49 2015-11-23 12:24 UploadDemo\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2015-11-23 12:24 UploadDemo\.settings\org.eclipse.wst.jsdt.ui.superType.name
文件 1840 2015-12-01 10:10 UploadDemo\build\classes\com\upload\demo\UploadServlet$1.class
文件 482 2015-12-01 10:10 UploadDemo\build\classes\com\upload\demo\UploadServlet$UploadStatusListener.class
文件 5994 2015-12-01 10:10 UploadDemo\build\classes\com\upload\demo\UploadServlet.class
文件 5535 2015-12-01 10:10 UploadDemo\src\com\upload\demo\UploadServlet.java
文件 39 2015-11-23 12:24 UploadDemo\WebContent\me
文件 886 2015-11-24 09:26 UploadDemo\WebContent\upload.jsp
文件 57779 2015-05-31 17:45 UploadDemo\WebContent\WEB-INF\lib\commons-fileupload-1.2.1.jar
文件 65621 2015-11-27 14:15 UploadDemo\WebContent\WEB-INF\lib\commons-io-1.2.jar
文件 321639 2015-06-03 15:03 UploadDemo\WebContent\WEB-INF\lib\httpcore-4.4.jar
文件 283561 2015-10-08 15:22 UploadDemo\WebContent\WEB-INF\lib\xUtils-2.6.14.jar
文件 922 2015-11-23 12:40 UploadDemo\WebContent\WEB-INF\web.xm
目录 0 2015-12-01 09:49 UploadDemo\build\classes\com\upload\demo
目录 0 2015-12-01 09:49 UploadDemo\build\classes\com\upload
目录 0 2015-11-23 12:40 UploadDemo\src\com\upload\demo
目录 0 2015-12-01 09:49 UploadDemo\build\classes\com
目录 0 2015-11-23 12:40 UploadDemo\src\com\upload
目录 0 2015-12-01 09:48 UploadDemo\WebContent\WEB-INF\lib
目录 0 2015-12-01 09:49 UploadDemo\build\classes
目录 0 2015-11-23 12:40 UploadDemo\src\com
目录 0 2015-11-23 12:24 UploadDemo\WebContent\me
目录 0 2015-11-24 09:22 UploadDemo\WebContent\WEB-INF
目录 0 2015-11-23 12:24 UploadDemo\.settings
............此处省略7个文件信息
- 上一篇:时间片轮转调度算法
- 下一篇:office2010卸载工具
相关资源
- Socket实现文件上传,含多线程
- qt ftp上传
- qt用QCustomPlot实现鼠标实时回调xy轴数
- MVC图片上传
- WCF 聊天室,wcf 回调 TCP双工
- 远程文件木马源代码非常详细
-
纯js弹出层la
yer跨if rame完美回调 - 在delphi xe7中调用七牛云存储api上传演
- upload-labs 上传训练
- 百度文库批量上传软件.pptx
- 使用commons-fileupload实现的文件上传和
- SpringBoot文件上传源码
- Web版学生作业上传系统(源码)
- 应用服务器直接上传视频文件到抖英
- P2P 文件上传
- 上传图片.zip
- Burp Suite Pro 2最新版 破解文件(原版文
- VC中回调函数(callback)深入解析
- ESP8266模块 上传数据 到 乐联网和反
- QT开发的视频采集并定时上传到服务器
- 七牛js文件上传,含后台代码,获取
- callback手把手教你写回调函数源代码
- 多类型文件上传图片自动在客户端压
- 百度webuploader多上传
- WCF文件简单上传服务器
- Ecshop添加商品相册批量上传插件
- FTP客户端完整版
- redupload多文件上传控件 dll
- 精密称重AD7730读写程序,并通过串口
- Netty+H5实现实时进度条文件上传,支持
评论
共有 条评论