package com.sys.tools;
//多文件上传的Java端的类
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.util.List;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FileUploadAction extends HttpServlet {
// 限制文件的上传大小
private int maxPostSize = 100 * 1024 * 1024;
//文件上传地址
private String uploadPath;
public FileUploadAction() {
//从配置文件中取得文件上传地址
setUploadPath();
}
public String getUploadPath() {
return uploadPath;
}
public void setUploadPath() {
//从配置文件中取得文件上传地址
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// ServerAddress serverAddressBean=(ServerAddress)context.getBean("ServerAddressBean");
// this.uploadPath = serverAddressBean.getUploadPath().trim();
this.uploadPath = "c:/";
}
// 文件上传
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException
{
this.doUploadAdd(request, response);
}
private void logger(String info) {
System.out.println(info);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doUploadAdd(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doUploadAdd(request, response);
}
private void doUploadAdd(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
logger("begin to upload");
try {
request.setCharacterEncoding("UTF-8");// 防止文件名称带有汉字后传到服务器乱码
//建立文件夹
this.makeDir(uploadPath);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
saveFiletoServer(request,response,uploadPath);
}
private void doUploadDelete(HttpServletRequest request,HttpServletResponse response) throws ServletException
{
String dirtyStr = "";//需要删除的文件名(单个文件)或文件夹名列表
try
{
if (request.getParameter("dirtyStr") != null) {
dirtyStr=URLDecoder.decode(request.getParameter("dirtyStr"),"utf-8");//前台ENCODE,后台DECODE
logger("删除的文件(夹)为:" + dirtyStr);
}
}
catch (Exception e) {
// TODO Auto-generated catch block
logger(e.getMessage());
}
try {
// new DeleteFiles().DeleteModifyFiles(dirtyStr, uploadPath);
} catch (Exception e) {
logger(e.getMessage());
}
}
//保存文件到服务器中
private void saveFiletoServer(HttpServletRequest request,HttpServletResponse response,String uploadPath)
{
// 操作文件
response.setContentType("text/html; charset=UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 4);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(maxPostSize);
logger("request========" + ObjectUtils.toString(request));
List fileItems = null;
try {
fileItems = upload.parseRequest(request);
logger("============" + ObjectUtils.toString(fileItems));
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
log(item.toString());
if (!item.isFormField()) {
String name = item.getName();
logger("上传的文件名 = " + name);
try {
item.write(new File(uploadPath + name));
} catch (Exception ex) {
logger(ex.getMessage());
}
}
}
} catch (FileUploadException ex1) {
logger("FileUploadException->" + ex1.getMessage());
}
}
// 建立文件夹路径
private boolean makeDir(String uploadPath) {
boolean isOK = false;
try {
File file = new File(uploadPath);
file.mkdirs();
isOK = true;
} catch (Exception e) {
isOK = false;
} finally {
return isOK;
}
}
// 建立文件夹路径
private boolean makeDirs(String uploadPath, String newDocStr) {
boolean isOK = false;
File file;
String[] temp;
try {
temp = newDocStr.split(",");
for (int i = 0; i < temp.length; i++) {
file = new File(uploadPath + temp[i] + "\\");
file.mkdirs();
}
isOK = true;
} catch (Exception e) {
isOK = false;
} finally {
return isOK;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
flex4+spring+hibernat实例
共181个文件
gif:66个
png:22个
swf:16个
4星 · 超过85%的资源 需积分: 3 85 下载量 151 浏览量
2010-07-29
17:09:54
上传
评论 1
收藏 4.83MB RAR 举报
温馨提示
flex4+spring+hibernat实例项目,采用myeclipse+spring+hibernate开发,例子非常完整
资源推荐
资源详情
资源评论
收起资源包目录
flex4+spring+hibernat实例 (181个子文件)
.actionScriptProperties 3KB
1.bmp 192KB
TSysDeparmtmentDAO.class 6KB
FileUploadAction.class 6KB
UploadServiceImpl.class 4KB
FileUtils.class 3KB
UploadControllerImpl.class 3KB
HibernateSessionFactory.class 3KB
ServerReply.class 2KB
SpringFactory$SpringFactoryInstance.class 2KB
TSysDeparmtment.class 2KB
SpringFactory.class 2KB
FileInfo.class 1KB
FilePart.class 1KB
UploadController.class 642B
UploadService.class 457B
.classpath 6KB
org.eclipse.wst.common.component 541B
org.eclipse.wst.jsdt.ui.superType.container 49B
history.css 365B
history.css 365B
.DS_Store 6KB
.flexProperties 415B
pepdemass.gif 1KB
pepdemass.gif 1KB
role.gif 1KB
role.gif 1KB
log.gif 1KB
log.gif 1KB
2.gif 1KB
2.gif 1KB
1.gif 1KB
1.gif 1KB
dep.gif 1KB
dep.gif 1KB
pepdem.gif 1KB
pepdem.gif 1KB
emp.gif 1KB
emp.gif 1KB
save.gif 1KB
save.gif 1KB
rtf-createlink.gif 1KB
user.gif 1KB
user.gif 1KB
pass.gif 1KB
pass.gif 1KB
add.gif 945B
.gif 945B
add.gif 945B
.gif 945B
dl.gif 940B
dl.gif 940B
up.gif 909B
up.gif 909B
rtf-unlink.gif 648B
edit.gif 628B
edit.gif 628B
rtf-insertimage.gif 615B
delete.gif 596B
delete.gif 596B
rtf-redo.gif 543B
rtf-undo.gif 540B
rtf-tableunmergecell.gif 386B
rtf-table.gif 371B
rtf-tablemergecell.gif 371B
rtf-tableproperties.gif 354B
ok.gif 202B
ok.gif 202B
rtf-indent.gif 191B
rtf-insertorderedlist.gif 189B
rtf-outdent.gif 188B
query.gif 169B
query.gif 169B
rtf-tabledelcol.gif 152B
rtf-tableaddcol.gif 150B
rtf-tabledelrow.gif 147B
rtf-tableaddrow.gif 145B
rtf-underline.gif 133B
rtf-bold.gif 132B
rtf-insertunorderedlist.gif 130B
rtf-italic.gif 119B
nexpage.gif 90B
nextpage.gif 90B
prepage.gif 90B
nextpage.gif 90B
prepage.gif 90B
rtf-justifyright.gif 67B
rtf-justifycenter.gif 67B
rtf-justifyleft.gif 67B
index.template.html 5KB
MainWindows.html 5KB
historyFrame.html 798B
historyFrame.html 798B
Index.html 481B
FileUploadAction.java 6KB
TSysDeparmtmentDAO.java 5KB
UploadServiceImpl.java 4KB
HibernateSessionFactory.java 3KB
FileUtils.java 3KB
UploadControllerImpl.java 2KB
共 181 条
- 1
- 2
资源评论
- test0071232016-01-14例子不错,可以使用
- qqdongxulove2013-04-08在flex builder4中引入报错,不知怎么搞?
xs_gxjcom
- 粉丝: 3
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功