*
*/
package net.hlj.chOA.action
import java.io.File
import java.io.UnsupportedEncodingException
import java.sql.SQLException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Iterator
import java.util.List
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import net.hlj.chOA.dao.DangAnDao
import net.hlj.chOA.dao.LogDao
import net.hlj.chOA.dao.ZiYuanDao
import net.hlj.chOA.model.DangAn
import net.hlj.chOA.model.User
import net.hlj.chOA.model.ZiYuan
import net.hlj.chOA.util.GetId
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
import org.apache.struts.action.ActionForm
import org.apache.struts.action.ActionForward
import org.apache.struts.action.ActionMapping
import org.apache.struts.actions.DispatchAction
/**
* @author lcy
*
*/
public class ZiYuanAction extends DispatchAction {
public ActionForward addZiYuan(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String title = ""
String fileupload=""
SimpleDateFormat fmt = new SimpleDateFormat("yyMMddhhssmmSSS")
Date now = new Date()
String date = fmt.format(now)
String filePath = request.getSession().getServletContext().getRealPath(
"/")
String uploadPath = filePath + "upload\\uploadZiYuan\\"
String tempPath = filePath + "upload\\uploadZiYuan\\" + "uploadTemp"
String suffix = null
if (!new File(uploadPath).isDirectory()) {
new File(uploadPath).mkdirs()
}
DiskFileItemFactory factory = new DiskFileItemFactory()
factory.setSizeThreshold(4194304)// 设置初始化内存,如果上传的文件超过该大小,将不保存到内存,而且硬盘中(单位:byte)
File fileTemp = new File(tempPath)// 建立临时目录
fileTemp.mkdir()
factory.setRepository(fileTemp)
ServletFileUpload upload = new ServletFileUpload(factory)
upload.setSizeMax(4194304)// 设置客户端最大上传,-1为无限大(单位:byte)
try {
List<FileItem>items = upload.parseRequest(request)
Iterator<FileItem>i = items.iterator()
String[] rightType = {".gif", ".jpeg", ".doc", ".xls",
".pdf", ".txt", ".rar" }
while (i.hasNext()) {
FileItem fi = (FileItem) i.next()
if (fi.isFormField()) {
try {
if (fi.getFieldName().equals("title")) {
title = fi.getString("UTF-8")
}
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e)
}
} else {
String fileName = fi.getName()
int l = fileName.length()
if (!fileName.equals("")) {
int pos = fileName.lastIndexOf(".")
// suffix = fileName.substring(l - 4, l)
suffix = fileName.substring(pos, l)
boolean result = false
String ext = fileName.substring(fileName
.lastIndexOf("."))
for (int j = 0j <rightType.lengthj++) {
if (ext.toLowerCase().equals(rightType[j])) {
result = true
break
}
}
if (!result) {
request.setAttribute("error", "上传文件类型有误!")
return mapping.findForward("addZiyuan")
}
// if (!suffix.equalsIgnoreCase(".jpg") &&
// !suffix.equalsIgnoreCase(".gif")
// &&!suffix.equalsIgnoreCase(".png") &&
// !suffix.equalsIgnoreCase(".bmp")) {
// request.setAttribute("message", "上传文件类型有误!")
// return mapping.findForward("danganList")
// }
if (fileName != null) {
File savedFile = new File(uploadPath, date + suffix)
try {
fi.write(savedFile)
fileupload = "upload/uploadZiYuan/" + date
+ suffix
} catch (Exception e) {
log.error(e.getMessage(), e)
}
}
}
}
}
} catch (FileUploadException e) {
log.error(e.getMessage(), e)
}
ZiYuan zy=new ZiYuan()
zy.setTitle(title)
zy.setUpload(fileupload)
ZiYuanDao zyDao=new ZiYuanDao()
try {
zyDao.addZiYuan(zy, this.servlet.getServletContext())
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
LogDao lDao=new LogDao()
//删除操做添加日志 isDel 0是增加,1是删除,2是修改,3是审批
User user1 =(User)request.getSession().getAttribute("userBean")
String ip=request.getLocalAddr()
int id=GetId.getId("ziyuan", this.servlet.getServletContext())
try {
lDao.addLogMe("ziyuan", id , ip, user1.getName(), user1.getId(), 0, this.servlet.getServletContext())
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return mapping.findForward("ziyuanList")
}
public ActionForward deleteZiYuan(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String[] ids= request.getParameterValues("id")
ZiYuanDao zyDao=new ZiYuanDao()
LogDao lDao=new LogDao()
for(int i=0i<ids.lengthi++){
try {
zyDao.deleteZiYuan(Integer.parseInt(ids[i]), this.servlet.getServletContext())
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
User user =(User)request.getSession().getAttribute("userBean")
String ip=request.getLocalAddr()
try {
lDao.addLogMe("tbl_users",Integer.parseInt(ids[i]), ip, user.getName(), user.getId(), 1, this.servlet.getServletContext())
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
return mapping.findForward("ziyuanList")
}
public ActionForward updateZiYuan(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, SQLException {
String id=""
String title = ""
String fileupload=""
SimpleDateFormat fmt = new SimpleDateFormat("yyMMddhhssmmSSS")
Date now = new Date()
String date = fmt.format(now)
String filePath = request.getSession().getServletContext().getRealPath(
"/")
String uploadPath = filePath + "upload\\uploadZiYuan\\"
String tempPath = filePath + "upload\\uploadZiYuan\\" + "uploadTemp"
String suffix = null
if (!new File(uploadPath).isDirectory()) {
new File(uploadPath).mkdirs()
}
DiskFileItemFactory factory = new DiskFileItemFactory()
factory.setSizeThreshold(4194304)// 设置初始化内存,如果上传的文件超过该大小,将不保存到内存,而且硬盘中(单位:byte)
File fileTemp = new File(tempPath)// 建立临时目录
fileTemp.mkdir()
factory.setRepository(fileTemp)
ServletFileUpload upload = new ServletFileUpload(factory)
upload.setSizeMax(4194304)// 设置客户端最大上传,-1为无限大(单位:byte)
try {
List<FileItem>items = upload.parseRequest(request)
Iterator<FileItem>i = items.iterator()
String[] rightType = {".gif", ".jpeg", ".doc", ".xls",
".pdf", ".txt", ".rar" }
while (i.hasNext()) {
FileItem fi = (FileItem) i.next()
if (fi.isFormField()) {
try {
if (fi.getFieldName().equals("title")) {
title = fi.getString("UTF-8")
}
if (fi.getFieldName().equals("id")) {
id = fi.getString("UTF-8")
}
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e)
}
} else {
String fileName = fi.getName()
int l = fileName.length()
if (!fileName.equals("")) {
int pos = fileName.lastIndexOf(".")
// suffix = fileName.substring(l - 4, l)
suffix = fileName.substring(pos, l)
boolean result = false
String ext = fileName.substring(fileName
.lastIndexOf("."))
for (int j = 0j <rightType.lengthj++) {
if (ext.toLowerCase().equals(rightType[j])) {
result = true
break
}
}
if (!result) {
request.setAttribute("error", "上传文件类型有误!")
request.setAttribute("id", id)
return mapping.findForward("selectZiyuan")
}
// if (!suffix.equalsIgnoreCase(".jpg") &&
// !suffix.equalsIgnoreCase(".gif")
// &&!suffix.equalsIgnoreCase(".png") &&
// !suffix.equalsIgnoreCase(".bmp")) {
// request.setAttribute("message", "上传文件类型有误!")
// return mapping.findForward("danganList")
// }
if (fileName != null) {
File savedFile = new File(uploadPath, date + suffix)
try {
fi.write(savedFile)
fileupload = "upload/uploadZiYuan/" + date
+ suffix
} catch (Exception e) {
log.error(e.getMessage(), e)
}
}
}else{
//这里写如果用户没有重写添加附件则保持原来的附件
ZiYuanDao zyDao = new ZiYuanDao()
ZiYuan zy=(ZiYuan)zyDao.selectZiYuanById(Integer.parseInt(id), this.servlet.getServletContext()).get(0)
fileupload=zy.getUpload()
}
}
}
} catch (FileUploadException e) {
log.error(e.getMessage(), e)
}
ZiYuan zy=new ZiYuan()
zy.setId(Integer.parseInt(id))
zy.setTitle(title)
zy.setUpload(fileupload)
ZiYuanDao zyDao=new ZiYuanDao()
try {
zyDao.updateZiYuan(zy, this.servlet.getServletContext())
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
LogDao lDao=new LogDao()
//删除操做添加日志 isDel 0是增加,1是删除,2是修改,3是审批
User user1 =(User)request.getSession().getAttribute("userBean")
String ip=request.getLocalAddr()
try {
lDao.addLogMe("ziyuan", Integer.parseInt(id) , ip, user1.getName(), user1.getId(), 2, this.servlet.getServletContext())
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return mapping.findForward("ziyuanList")
}
}
你的意思是拷贝吗,还是上传到服务器什么的import java.io.*
/**
* 复制文件夹或文件夹
*/
public class CopyDirectory {
// 源文件夹
static String url1 = "f:/photos"
// 目标文件夹
static String url2 = "d:/tempPhotos"
public static void main(String args[]) throws IOException {
// 创建目标文件夹
(new File(url2)).mkdirs()
// 获取源文件夹当前下的文件或目录
File[] file = (new File(url1)).listFiles()
for (int i = 0i <file.lengthi++) {
if (file[i].isFile()) {
// 复制文件
copyFile(file[i],new File(url2+file[i].getName()))
}
if (file[i].isDirectory()) {
// 复制目录
String sourceDir=url1+File.separator+file[i].getName()
String targetDir=url2+File.separator+file[i].getName()
copyDirectiory(sourceDir, targetDir)
}
}
}
// 复制文件
public static void copyFile(File sourceFile,File targetFile)
throws IOException{
// 新建文件输入流并对它进行缓冲
FileInputStream input = new FileInputStream(sourceFile)
BufferedInputStream inBuff=new BufferedInputStream(input)
// 新建文件输出流并对它进行缓冲
FileOutputStream output = new FileOutputStream(targetFile)
BufferedOutputStream outBuff=new BufferedOutputStream(output)
// 缓冲数组
byte[] b = new byte[1024 * 5]
int len
while ((len =inBuff.read(b)) != -1) {
outBuff.write(b, 0, len)
}
// 刷新此缓冲的输出流
outBuff.flush()
//关闭流
inBuff.close()
outBuff.close()
output.close()
input.close()
}
// 复制文件夹
public static void copyDirectiory(String sourceDir, String targetDir)
throws IOException {
// 新建目标目录
(new File(targetDir)).mkdirs()
// 获取源文件夹当前下的文件或目录
File[] file = (new File(sourceDir)).listFiles()
for (int i = 0i <file.lengthi++) {
if (file[i].isFile()) {
// 源文件
File sourceFile=file[i]
// 目标文件
File targetFile=new
File(new File(targetDir).getAbsolutePath()
+File.separator+file[i].getName())
copyFile(sourceFile,targetFile)
}
if (file[i].isDirectory()) {
// 准备复制的源文件夹
String dir1=sourceDir + "/" + file[i].getName()
// 准备复制的目标文件夹
String dir2=targetDir + "/"+ file[i].getName()
copyDirectiory(dir1, dir2)
}
}
}
}