资源简介

图片上传功能几乎是每一个项目都必备的,而没遇到一次都要重新写一篇,就陷入了重复造轮子的圈子中,所以今天把这个上传图片的小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.xml

     文件         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\meta-INF\MANIFEST.MF

     文件        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.xml

     目录          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\meta-INF

     目录          0  2015-11-24 09:22  UploadDemo\WebContent\WEB-INF

     目录          0  2015-11-23 12:24  UploadDemo\.settings

............此处省略7个文件信息

评论

共有 条评论