文件上传,下载的方法:

上传代码

 /**
* 文件上传、保存
*
* @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. 移动端点击a链接出现蓝色背景问题解决

    a:link, a:active, a:visited, a:hover { background: none; -webkit-tap-highlight-color: rgba(0,0,0,0); ...

  2. .NET开源工作流RoadFlow-表单设计-HTML编辑器

    roadflow目前采用的html编辑器为ueditor编辑器(和表单设计器的编辑器相同). 绑定字段:与数据表的某个字段对应. 宽度:编辑器的宽度. 高度:编辑器的高度. 运行效果如下:

  3. 14_synchronized深入

    [脏读] 对于对象同步和异步的方法,我们在设计程序的时候,一定要考虑问题的整体,不然会出现不一致的错误,最经典的错误的就是脏读(dirty read). [实例代码] package com.higg ...

  4. css Media Query详解

    Media Queries详解 Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: 1 <link href="css/re ...

  5. UML视频

    https://www.bilibili.com/video/av34973179/?p=1 北京 圣思园 UML视频

  6. leetcode 62、Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  7. HDU-3092 Least common multiple---数论+分组背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3092 题目大意: 有一个数字n,现在要把它分解成几个数字相加!然后这几个数字有最小公倍数,题目目的是 ...

  8. Android(java)学习笔记54:Android 调用Vibrator震动功能

    1. 之前我编写的代码是如下: package com.himi.vibrate; import android.app.Activity; import android.app.Service; i ...

  9. Batch Normalization:Accelerating Deep Network Training by Reducing Internal Covariate Shift(BN)

    internal covariate shift(ics):训练深度神经网络是复杂的,因为在训练过程中,每层的输入分布会随着之前层的参数变化而发生变化.所以训练需要更小的学习速度和careful参数初 ...

  10. @WebListener 注解方式实现监听(eclipse和idea)

    eclipse进行演示: 1.创建 Dynamic Web Project ,Dynamic Web module version选择3.0 2.在自动生成 的web.xml配置,增加 metadat ...