Java实现文件的上传下载
文件上传,下载的方法:
上传代码
/**
* 文件上传、保存
*
* @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实现文件的上传下载的更多相关文章
- Java实现文件的上传下载(含源代码和jar包)
1.需要使用的jar包 链接:https://pan.baidu.com/s/1IaxQRSwfzxDpe4w4JiaEKw 提取码:xwtz 2.如果想实现文件的下载,需要创建一张表,表的结构为 i ...
- java实现文件的上传和下载
1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...
- java客户端文件的上传和下载
java客户端文件的上传和下载 //上传 public JTable upload(String id){ JTable table=new JTable(); System.out.println( ...
- JAVA中使用FTPClient上传下载
Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...
- Spring实现文件的上传下载
背景:之前一直做的是数据库的增删改查工作,对于文件的上传下载比较排斥,今天研究了下具体的实现,发现其实是很简单.此处不仅要实现单文件的上传,还要实现多文件的上传. 单文件的下载知道了,多文件的下载呢? ...
- SocketIo+SpringMvc实现文件的上传下载
SocketIo+SpringMvc实现文件的上传下载 socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: GIT地址 ...
- JAVAWEB之文件的上传下载
文件上传下载 文件上传: 本篇文章使用的文件上传的例子使用的都是原生技术,servelt+jdbc+fileupload插件,这也是笔者的习惯,当接触到某些从未接触过的东西时,总是喜欢用最原始的东西将 ...
- SSM框架之中如何进行文件的上传下载
SSM框架的整合请看我之前的博客:http://www.cnblogs.com/1314wamm/p/6834266.html 现在我们先看如何编写文件的上传下载:你先看你的pom.xml中是否有文件 ...
- 使用Fileupload完成文件的上传下载
目录 使用Fileupload完成文件的上传下载 为什么需要进行文件上传下载? 引入jar包 文件上传 注意事项 编写一个简单的文件上传jsp页面 编写Servlet Student类用于封装数据,后 ...
随机推荐
- 【Linux】Windows与Linux之间的文件共享(基于网络)
切记:Linux的安全机制的存在--iptables和selinux. 一.操作前提 1.1 从Windows能够ping通Linux 1.2 关闭Linux防火墙 command1:/etc/ini ...
- mysql的sql_mode介绍和修改
原文链接: http://blog.csdn.net/wulantian/article/details/8905573 mysql目录下有一个配置文件my.conf. mysql数据库有一个环境 ...
- C# 中关于radiobutton控件的使用
在一个Form窗口中定义了3个radiobutton,radioButton1.radioButton2和radioButton3,以及button1和button2(这里可以是其他控件) 为了实现单 ...
- Python变量和数据类型(入门2)
转载请标明出处: http://www.cnblogs.com/why168888/p/6400809.html 本文出自:[Edwin博客园] Python变量和数据类型 一.整数 int = 20 ...
- Bootstrap Table的使用小结
1.Jquery中的一些东西学习一下子,补充完善一下,毕竟有些时候没有使用到 这个方式很有用,在使用bootstrap table的时候,选择当前已经选择的节点的事件中的ID的值 当前rows中有很多 ...
- 用户级线程demo
http://blog.csdn.net/dabing69221/article/details/17426953 前言: 前几天复习了一下多线程,发现有许多网上讲的都很抽象,所以,自己把网上的一些案 ...
- 【洛谷5292】[HNOI2019] 校园旅行(思维DP)
点此看题面 大致题意: 给你一张无向图,每个点权值为\(0\)或\(1\),多组询问两点之间是否存在一条回文路径. 暴力\(DP\) 首先,看到\(n\)如此之小(\(n\le5000\)),便容易想 ...
- 【[AH2017/HNOI2017]礼物】
题目 又是我不会做的题了 看看柿子吧 \[\sum(a_i+c-b_i)^2\] 最小化这个柿子 之所以不写下标是因为我们这个\(\{a\},\{b\}\)可以循环同构 那就开始化吧 \[\sum(a ...
- div可编辑框,去除粘贴文字样式😄
上个月做了个聊天的需求(网页版的).说到聊天都想到输入框,说到输入框都会想到input,但是input标签是不支持插入图片的(包括areatext标签).查阅了一些资料就看到div标签有一个属性con ...
- 【luogu P1494 [国家集训队]小Z的袜子】 题解
题目链接:https://www.luogu.org/problemnew/show/P1494 #include <cstdio> #include <algorithm> ...