文件上传,下载的方法:

上传代码

 /**
* 文件上传、保存
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
@SuppressWarnings("unchecked")
public ActionForward tzupload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseActionForm cForm = (BaseActionForm) form;
Dto reDto = cForm.getParamAsDto(request);
String uuid = new UUIDGenerator().create();
String ggid = new UUIDGenerator().create();
UserInfoVo userInfo = WebUtils.getSessionContainer(request).getUserInfo();
String gglx = "ggxx";
reDto.put("uuid", uuid);
reDto.put("ggid", ggid);
reDto.put("yxbz", "Y");
reDto.put("lrr_dm", userInfo.getUserid()); FormFile formfile = cForm.getFile1();
String filename = formfile.getFileName();// 获取文件的名字
// 获取当前时间
Date date = new Date();
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
String dqsj = simpledate.format(date);
String urlfile = readProperties.getProperties().getProperty(
"FILEUPLOAD1")
+ formfile;
final String wjlj = readProperties.getProperties().getProperty(
"FILEUPLOAD2")
+ formfile; if (filename.length() > 0) {
reDto.put("wjlj", wjlj);
reDto.put("wjmc", filename);
reDto.put("ggid", ggid);
reDto.put("gglx", gglx);
reDto.put("scsj", dqsj);
reDto.put("lrrq", dqsj);
reDto.put("yxbz", "Y");
reDto.put("lrr_dm", reDto.getAsString("lrr_dm"));
mobilesBsService.updatewjlj(reDto); File file = new File(urlfile);
if (file.exists()) {
file.mkdir();// 创建路径
}
File filecreate = new File(urlfile);
// 检查路径是否
if (!filecreate.exists()) {
FileOutputStream out = new FileOutputStream(filecreate);
out.write(formfile.getFileData());
out.flush();
out.close();
} else {
FileOutputStream out = new FileOutputStream(filecreate);
out.write(formfile.getFileData());
out.flush();
out.close();
} }
Dto pDto = mobilesBsService.saveTzggDatas(reDto);
pDto.put("gglx", gglx);
mobilesBsService.createHtml(pDto);
String wjname = ggid + ".html";
pDto.put("wjname", wjname);
mobilesBsService.saveHtml(pDto); setOkTipMsg("文件上传成功", response);
return mapping.findForward(null); }

下载代码

 /**
* 文件下载
*
* @param mapping
* @param form
* @param request
* @param reponse
* @return
* @throws Exception
*/
public ActionForward downloadfile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception, Exception {
// private final static String
String filename = request.getParameter("wjmc");
filename = new String(filename.getBytes("ISO-8859-1"), "utf-8");
String filesavepath = request.getRealPath("/upload/fileupload1");
//String path = findFileSavePathByFileName(filename,filesavepath);
//得到要下载的文件
File file = new File(filesavepath+"\\"+filename);
if(!file.exists()){
request.setAttribute("message", "要下载的文件已删除");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
//处理文件名
String realname = filename.substring(filename.indexOf("_")+1);
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
//创建文件输入流
FileInputStream in = new FileInputStream(file);
//创建输出流
OutputStream out = response.getOutputStream();
//创建缓存区
byte buffer[] = new byte[1024];
int len = 0;
while((len = in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
//关闭输出流
out.close();
return mapping.findForward(null);
}
     /**
* 通过文件名和上传文件根目录找出要下载的文件
* @param filename
* @param saveRootPath
* @return
*/
public String findFileSavePathByFileName(String filename,
String saveRootPath) {
int hashcode = filename.hashCode();
int dir1 = hashcode & 0xf; // 0--15
int dir2 = (hashcode & 0xf0) >> 4; // 0-15
String dir = saveRootPath + "\\" + dir1 + "\\" + dir2; // upload\2\3
// upload\3\5
File file = new File(dir);
if (!file.exists()) {
// 创建目录
file.mkdirs();
}
return dir;
}

Java实现文件的上传下载的更多相关文章

  1. Java实现文件的上传下载(含源代码和jar包)

    1.需要使用的jar包 链接:https://pan.baidu.com/s/1IaxQRSwfzxDpe4w4JiaEKw 提取码:xwtz 2.如果想实现文件的下载,需要创建一张表,表的结构为 i ...

  2. java实现文件的上传和下载

    1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...

  3. java客户端文件的上传和下载

    java客户端文件的上传和下载 //上传 public JTable upload(String id){ JTable table=new JTable(); System.out.println( ...

  4. JAVA中使用FTPClient上传下载

    Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...

  5. Spring实现文件的上传下载

    背景:之前一直做的是数据库的增删改查工作,对于文件的上传下载比较排斥,今天研究了下具体的实现,发现其实是很简单.此处不仅要实现单文件的上传,还要实现多文件的上传. 单文件的下载知道了,多文件的下载呢? ...

  6. SocketIo+SpringMvc实现文件的上传下载

    SocketIo+SpringMvc实现文件的上传下载 socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: GIT地址 ...

  7. JAVAWEB之文件的上传下载

    文件上传下载 文件上传: 本篇文章使用的文件上传的例子使用的都是原生技术,servelt+jdbc+fileupload插件,这也是笔者的习惯,当接触到某些从未接触过的东西时,总是喜欢用最原始的东西将 ...

  8. SSM框架之中如何进行文件的上传下载

    SSM框架的整合请看我之前的博客:http://www.cnblogs.com/1314wamm/p/6834266.html 现在我们先看如何编写文件的上传下载:你先看你的pom.xml中是否有文件 ...

  9. 使用Fileupload完成文件的上传下载

    目录 使用Fileupload完成文件的上传下载 为什么需要进行文件上传下载? 引入jar包 文件上传 注意事项 编写一个简单的文件上传jsp页面 编写Servlet Student类用于封装数据,后 ...

随机推荐

  1. JSTL标签概述

    什么是JSTL JSP 标准标记库(JSP Standard Tag Library,JSTL)是一个实现 Web 应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断.数据管理格式化. ...

  2. 15分钟完成基于Azure公有云搭建远程测试环境

  3. Excel VBA 复制

    将 Sheet1 复制到 Sheet3 后面时,实现方法如下: Worksheets("Sheet1").Copy After:=Worksheets("Sheet3&q ...

  4. Spark天堂之门解密

    本课主题 什么是 Spark 的天堂之门 Spark 天堂之门到底在那里 Spark 天堂之门源码鉴赏 引言 Spark 天堂之门就是SparkContext,这篇文章会从 SparkContext ...

  5. c++中的const用法(很详细)——转

    http://www.cnblogs.com/ymy124/archive/2012/04/16/2451433.html const给人的第一印象就是定义常量. (1)const用于定义常量. 例如 ...

  6. 用一个实际例子理解Docker volume工作原理

    要了解Docker Volume,首先我们需要理解Docker文件系统的工作原理.Docker镜像是由多个文件系统的只读层叠加而成.当一个容器通过命令docker run启动时,Docker会加载只读 ...

  7. 数据库操作(c#)

    windows窗体程序中的数据库操作部分 //数据库连接串 internal static string connstring = "Data Source = 192.168.1.1; I ...

  8. PHP使用memcache长连接作为RPC客户端需要注意的地方

    memcache扩展版本 3.0.8 一. retry_interval $retry_interval 某个rpc服务器端失败后故障转移的时间,retry_interval的时间内,该节点会被一直标 ...

  9. 开始学习git

    今天看着廖雪峰的git使用教程开始学习git.不过没有将项目托管在github上,而是选择托管在了码云上. 看着明白做起来还是出了些问题,不过好在最后都解决了.果然眼高手低要不得. 试着将自己平时学习 ...

  10. Gym 101308I Inspection

    题意: 用最少的路径,覆盖掉所有的边,(点可以重复): 不是用最小路径覆盖,最小路径覆盖是覆盖点: 分析: 建图:入度<出度,说明这是个起点,从这里出发,入度>出度,说明从这里结束: 先找 ...