- 浏览: 102792 次
- 性别:
- 来自: 深圳
最新评论
-
qq1988627:
gaolegao2008 写道说的是个啥。有冲突怎么办呢。也不 ...
xfire与spring组合创建webservice -
gaolegao2008:
说的是个啥。有冲突怎么办呢。也不说个解决的办法。唉
xfire与spring组合创建webservice -
xiaoliefengfeng:
加油,为码农争光啊
我要的其实并不多 你为什么拒绝我 -
qq1988627:
huchaoqqqq 写道用的ext几版本做的呢,有没有相关资 ...
第一次使用Ext写系统 -
huchaoqqqq:
用的ext几版本做的呢,有没有相关资料,我也想最近弄下ext ...
第一次使用Ext写系统
package com.byd.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.tools.zip.ZipOutputStream; import com.byd.core.BaseAction; import com.byd.core.MyUtils; import com.byd.Page; import com.byd.bean.MyFile; @SuppressWarnings("serial") public class FileAction extends BaseAction { public static final String ROOT = "root\\"; private File myUpload; private String myUploadContentType; private String myUploadFileName; private String path; private String node; private List nodes; private Page page; private String name; private String[] paths; private boolean success; /** * 处理中文下载名 * * @return * @throws UnsupportedEncodingException */ public String getDownloadFileName() throws UnsupportedEncodingException { String named = new String(name.getBytes("ISO8859-1"),"utf-8" ); return named; } /** * 获得文件下载流 * * @return * @throws FileNotFoundException */ public InputStream getInputStream() throws FileNotFoundException { try{ return getServletContext().getResourceAsStream(ROOT + path + "/" + getDownloadFileName()); }catch(Exception ex){ ex.printStackTrace(); } return null; } /** * 下载文件 * * @return */ public String download() { return SUCCESS; } /** * 解压缩文件 * * @return */ public String decompressionFiles() { String rootPath = getSession().getServletContext().getRealPath("/"); rootPath += ROOT; File file = new File(rootPath); if(!file.exists()){ file.mkdirs(); } file = null; String extName, toPath, absPath; boolean flag = false; try { for (String path : paths) { file = new File(rootPath + path); if (file.isDirectory()) { continue; } extName = path.substring(path.lastIndexOf(".") + 1).toLowerCase(); toPath = rootPath + (((getPath() != null) && (getPath().length() > 0)) ? getPath().substring(1) : "") + "\\"; absPath = file.getAbsolutePath(); if ("zip".equals(extName)) { flag = MyUtils.decompressionZipFiles(absPath, toPath); } else if ("rar".equals(extName)) { flag = MyUtils.decompressionRarFiles(absPath, toPath); } } } catch (RuntimeException e) { flag = false; e.printStackTrace(); } finally { file = null; } setSuccess(flag); return SUCCESS; } /** * 多文件下载 * * @throws IOException */ public String downloadAll() throws IOException { // String rootPath = getSession().getServletContext().getRealPath("/"); return SUCCESS; } // public InputStream getZipInputStrean() throws IOException { // String rootPath = getSession().getServletContext().getRealPath("/"); // ZipOutputStream zosm = null; // FileOutputStream fosm = null; // File file = null; // String zipName = "Untitled.zip"; // // fosm = new FileOutputStream(rootPath + getPath() + "\\" + zipName); // zosm = new ZipOutputStream(fosm); // for (String path : paths) { // file = new File(rootPath + path); // MyUtils.compressionFiles(zosm, file, getPath()); // file = null; // } // return MyUtils.getZipInputStrean(zosm, file, getPath()); // } /** * 多文件压缩 * * @return * @throws IOException */ public String compressionFiles() throws IOException { String rootPath = getSession().getServletContext().getRealPath("/"); rootPath += ROOT; File filed = new File(rootPath); if(!filed.exists()){ filed.mkdirs(); } filed = null; ZipOutputStream zosm = null; FileOutputStream fosm = null; File file = null; try { String zipName = "Untitled.zip"; if (getPaths().length > 0) { String tempName = getPaths()[0]; int start = tempName.lastIndexOf("\\"); if (start != -1) { tempName = tempName.substring(start + 1); } zipName = tempName + ".zip"; zipName = MyUtils.checkFileName(zipName, rootPath + getPath() + "\\"); } fosm = new FileOutputStream(rootPath + getPath() + "\\" + zipName); zosm = new ZipOutputStream(fosm); for (String path : paths) { file = new File(rootPath + path); MyUtils.compressionFiles(zosm, file, getPath()); file = null; } setSuccess(true); } catch (IOException e) { setSuccess(false); e.printStackTrace(); throw e; } finally { file = null; if (zosm != null) { zosm.close(); } if (fosm != null) { fosm.close(); } } return SUCCESS; } /** * 多文件删除 * * @return */ public String deleteFiles() { String rootPath = getSession().getServletContext().getRealPath("/"); rootPath += ROOT; File file = new File(rootPath); if(!file.exists()){ file.mkdirs(); } file = null; boolean flag = false; try { for (String path : paths) { file = new File(rootPath + path); flag = MyUtils.delFiles(file); if (!flag) { break; } } } catch (RuntimeException e) { flag = false; e.printStackTrace(); } finally { file = null; } setSuccess(flag); return SUCCESS; } /** * 创建文件夹 * * @return */ public String createFolder() { String rootPath = getSession().getServletContext().getRealPath("/"); rootPath += ROOT; String createPath = rootPath + getPath() + "/"; setSuccess(MyUtils.mkDirectory(createPath + getName())); return SUCCESS; } /** * 上传文件 * * @return */ public String uploadFiles() { String rootPath = getSession().getServletContext().getRealPath("/"); rootPath += ROOT; String sp = rootPath + getPath(); MyUtils.mkDirectory(sp); setSuccess(MyUtils.upload(getMyUploadFileName(), sp, getMyUpload())); return SUCCESS; } /** * 2008-12-18-下午02:00:17 * * 功能:获得指定目录下的所有目录信息 * * @return * @throws IOException * @throws FileNotFoundException */ @SuppressWarnings("unchecked") public String getDirectories() throws FileNotFoundException, IOException { String rootPath = getSession().getServletContext().getRealPath("/"); rootPath += ROOT; File file = new File(rootPath); if(!file.exists()){ file.mkdirs(); } file = null; nodes = listFiles(rootPath, node, true); return SUCCESS; } /** * 2008-12-18-下午04:52:19 * * 功能:获得指定路径下大所有文件和文件夹信息,把数据封装到nodes返回 * * @param folder * 当前要访问的文件夹目录名称 * @param onlyDirectory * null:获得所有信息,true:只获得文件夹,false:只获得文件信息 * @return * @throws IOException * @throws FileNotFoundException */ @SuppressWarnings("unchecked") private List listFiles(String rootPath, String folder, boolean onlyDirectory) throws FileNotFoundException, IOException { List filelist = new ArrayList(); File[] arrFiles = new File(rootPath + folder).listFiles(); MyFile nd = null; if (arrFiles != null) { for (File f : arrFiles) { String id = f.getAbsolutePath(); nd = new MyFile(); nd.setId(id.substring(rootPath.length())); nd.setText(f.getName()); nd.setLeaf(f.isFile()); nd.setFileName(f.getName()); if (f.isFile()) { int size = new FileInputStream(f).available(); if (size > 1024) { nd.setFileSize((size / 1000f) + " KB"); } else { nd.setFileSize(size + " bytes"); } } else { nd.setFileSize("0 bytes"); } nd.setLastModifyDate(new Date(f.lastModified())); if (onlyDirectory && !f.isDirectory()) { continue; } filelist.add(nd); } } return filelist; } /** * 2008-12-18-下午05:17:38 * * 功能:获得指定文件夹下面的所有文件和文件夹信息 * * @return * @throws IOException * @throws FileNotFoundException */ public String getFiles() throws FileNotFoundException, IOException { String rootPath = getSession().getServletContext().getRealPath("/"); rootPath += ROOT; File file = new File(rootPath); if(!file.exists()){ file.mkdirs(); } file = null; page = new Page(); node = node == null ? "" : node; page.setRoot(this.listFiles(rootPath, node, false)); int length = new File(rootPath + node).list().length; page.setTotalProperty(length); return SUCCESS; } public String getNode() { return node; } public void setNode(String node) { this.node = node.equals("*") ? "" : node; // 处理根结点特殊id } public List getNodes() { return nodes; } public void setNodes(List files) { this.nodes = files; } public Page getPage() { return page; } public void setPage(Page page) { this.page = page; } public String getPath() { return path; } public void setPath(String path) throws UnsupportedEncodingException { this.path = URLDecoder.decode(path, "UTF-8"); } public File getMyUpload() { return myUpload; } public void setMyUpload(File myUpload) { this.myUpload = myUpload; } public String getMyUploadContentType() { return myUploadContentType; } public void setMyUploadContentType(String myUploadContentType) { this.myUploadContentType = myUploadContentType; } public String getMyUploadFileName() { return myUploadFileName; } public void setMyUploadFileName(String myUploadFileName) { this.myUploadFileName = myUploadFileName; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getName() { return name; } public void setName(String fileName) throws UnsupportedEncodingException { this.name = URLDecoder.decode(fileName, "UTF-8"); } public String[] getPaths() { return paths; } public void setPaths(String[] names) { this.paths = names; } }
<include file="struts-default.xml" /> <package name="simple" extends="struts-default"> <action name="download" class="fileAction" method="download"> <result name="success" type="stream"> <param name="contentType">application/octet-stream;charset=ISO8859-1</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">4096</param> </result> </action> </package>
发表评论
-
进制转换
2014-08-29 18:55 911package com.byd.core; imp ... -
xfire与spring组合创建webservice
2011-11-18 09:52 28731.导入xfire-core包(有冲突包,具体哪些包见附件 ... -
Java发送Email
2011-11-07 13:57 1000package byd.core; import java. ... -
天干地支算法
2011-10-10 11:44 1126/** * <url>http://en.w ... -
MD5
2011-09-27 09:34 714public class Encryptor { ... -
java运行可以执行文件
2011-08-15 10:44 732# package com.yymt.pf.rpc.loadb ... -
随记生成验证码
2011-08-06 09:28 931package com.hrm.houfei.utils; ... -
TLD生成
2011-08-06 09:16 963package j2ee.util; import ja ... -
页面解析
2011-08-06 09:13 836package com.atom.util; impor ... -
StringUtils 工具类
2011-08-06 09:11 1046package com.atom.util; impor ... -
重构HashTable
2011-08-06 09:03 871package com.atom.util; impor ... -
文件解析
2011-08-06 08:52 520package com.byd.portal.pagedesi ... -
工作流(workflow)
2011-08-06 08:39 1186package com.byd.mes.plugins.wor ... -
缓存处理工具类
2011-08-06 08:20 1package com.byd.mes.util.cache; ... -
缓存处理工具类
2011-08-06 08:20 1158package com.byd.mes.util.cache; ... -
Java执行javascript
2011-08-06 08:17 892package com.byd.mes.util.script ... -
文件处理工具类
2011-08-05 17:12 856/* * MyUtils.java * * */ ... -
对于不同数据库分页分查询析
2011-08-05 16:32 1010package zxc.utils; /** * 这 ... -
类转换
2011-08-05 16:30 773package zxc.utils; import ja ... -
数字转中文
2011-08-05 16:28 783package zxc.utils; import ja ...
相关推荐
struts2 文件上传下载
http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接
这个项目可将文件上传作为数据源,而且可以将查询的结果作为下载的文件
Struts2文件上传下载和表单重复提交问题
在struts2基础上实现的包括单文件、多文件的上传及下载
第八章:struts2文件上传下载.ppt
Struts2 文件上传下载代码 tomcat6.x运行 内有文件上传原理代码
通过struts2框架实现Struts2文件上传下载以及显示。综合实现数据的利用。
Struts2文件上传下载 上传为单个上传, 下载包括中文乱码的问题:前台传后台时中文乱码,下载时显示时中文乱码,以及URLEncoder
Struts2中文件上传与下载案例
struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!
struts2 文件上传下载的实现 ss2h包
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...