Java创建文件夹、创建文件、上传文件,下载文件
1、创建文件夹
/**
* 判断文件夹是否存在
* @param myPath
*/
public static void judeDirExists(File myPath) {
if (!myPath.exists()) {
myPath.mkdirs();//多级文件夹目录
//myPpath.mkdir();//单级文件夹目录
System.out.println("创建文件夹路径为:"+ myPath.getPath());
}
}
2、修改文件夹名称
/**
* 通过文件路径直接修改文件名
* @param filePath 需要修改的文件的完整路径
* @param newFileName 需要修改的文件的名称
* @return
*/
private static String modifyFileName(String filePath, String newFileName) {
File f = new File(filePath);
if (!f.exists()) { // 判断原文件是否存在
return null;
}
newFileName = newFileName.trim();
if ("".equals(newFileName)) // 文件名不能为空
return null;
String newFilePath = null;
if (f.isDirectory()) { // 判断是否为文件夹
newFilePath = filePath.substring(0, filePath.lastIndexOf("/")) + "/" + newFileName;
} else {
newFilePath = filePath.substring(0, filePath.lastIndexOf("/"))+ "/" + newFileName + filePath.substring(filePath.lastIndexOf("."));
}
File nf = new File(newFilePath);
if (!f.exists()) { // 判断需要修改为的文件是否存在(防止文件名冲突)
return null;
}
try {
f.renameTo(nf); // 修改文件名
} catch(Exception err) {
err.printStackTrace();
return null;
} return newFilePath;
}
3、文件附件形式下载
/**
* 下载文档文件
* @param request
* @param response
* @param file_name
* @throws IOException
*/
@RequestMapping(value = "/download_dex_privacy.do/{file_name}")
public void downloadDexDoc(@PathVariable("file_name") String file_name,HttpServletRequest request, HttpServletResponse response) throws IOException {
String path=request.getServletContext().getRealPath("/WEB-INF/classes/dex_doc");
System.out.println(path);
File file=new File(path+"/"+file_name);
// 获取输入流
InputStream bis = new BufferedInputStream(new FileInputStream(file));
// 假如以中文名下载的话
// 转码,免得文件名中文乱码
file_name = URLEncoder.encode(file_name, "UTF-8");
// 设置文件下载头
response.addHeader("Content-Disposition", "attachment;filename=" + file_name);
// 1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while ((len = bis.read()) != -1) {
out.write(len);
out.flush();
}
out.close();
}
4、上传文件
/**
* 添用户收款方式信息
* @param receipt
* @return
*/
@RequestMapping(value = "add.json")
@ResponseBody
public ReturnReceipt addReceipt(@RequestParam("file") MultipartFile file, String fundsPassword, Receipt receipt,HttpServletRequest request, HttpServletResponse response) {
BasePage_Model pageModel=SetDefaultPage(request);
ReturnReceipt returnReceipt=new ReturnReceipt();
if(pageModel.user_cookie_model.phone==null){
try {
response.sendRedirect("/login");
return null;
} catch (IOException e) {
logger.error("ReceiptController==>addReceipt==>未登录跳转异常==>"+e.getMessage());
}
}
if (file.isEmpty()) {
returnReceipt.status=ReturnReceipt.status_102;
returnReceipt.message="上传失败,请选择文件";
}else{
ReceiptMode receiptMode=new ReceiptMode();
receiptMode.id=receipt.receiptModeId;
ReturnReceiptMode returnReceiptMode=receiptModeDao.queryReceiptModeById(receiptMode);
if(returnReceiptMode.status==ReturnReceiptMode.status_100){
ReceiptMode receipt_mode=returnReceiptMode.receipt_mode;
String fileName= pageModel.user_cookie_model.phone+DateUtil.getTimeStamp();
boolean file_state=uploadFile(file,receipt_mode.name,fileName);
if(file_state){
Receipt _receipt=new Receipt();
_receipt.phone=pageModel.user_cookie_model.phone;
_receipt.receiptModeId=receipt.receiptModeId;
_receipt.receiptInfo=receipt.receiptInfo+","+fileName;
ReturnReceipt _returnReceipt=receiptDao.addReceipt(_receipt);
returnReceipt.status=_returnReceipt.status;
returnReceipt.message=_returnReceipt.message;
}else{
returnReceipt.status=ReturnReceipt.status_103;
returnReceipt.message="添加收款方式非法操作";
}
}else{
returnReceipt.status=ReturnReceipt.status_103;
returnReceipt.message="添加收款方式非法操作";
}
} return returnReceipt;
} public boolean uploadFile(MultipartFile file,String receiptName,String fileName){
String filePath = ConfigDefault.receipt_mode_url + "/" + receiptName + "/";
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
File dest = new File(filePath + fileName + ".png");
try {
file.transferTo(dest);
return true;
} catch (IOException e) {
logger.error("ReceiptController==>uploadFile==>IOException==>" + e.getMessage());
return false;
}
}
5、
Java创建文件夹、创建文件、上传文件,下载文件的更多相关文章
- 使用paramiko模块远程登录并上传或下载文件
1.paramiko安装 1)安装PyCrypto2.6 for Python 2.7 64bit.地址:http://www.voidspace.org.uk/python/modules.shtm ...
- ftp上传或下载文件工具类
FtpTransferUtil.java工具类,向ftp上传或下载文件: package utils; import java.io.File; import java.io.FileOutputSt ...
- 如何在Linux中使用sFTP上传或下载文件与文件夹
如何在Linux中使用sFTP上传或下载文件与文件夹 sFTP(安全文件传输程序)是一种安全的交互式文件传输程序,其工作方式与 FTP(文件传输协议)类似. 然而,sFTP 比 FTP 更安全;它通过 ...
- Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)
一.接收参数(postman发送) 1.form表单 @RequestParam("name") String name 会把传递过来的Form表单中的name对应 ...
- Linux学习笔记:使用ftp命令上传和下载文件
Linux中如何使用ftp命令,包括如何连接ftp服务器,上传or下载文件以及创建文件夹.虽然现在有很多ftp桌面应用(例如:FlashFXP),但是在服务器.SSH.远程会话中掌握命令行ftp的使用 ...
- 11、只允许在主目录下上传和下载文件,不允许用putty登录
创建用户xiao, 使其只允许在用户主目录 (/var/www/html)下上传和下载文件,不允许用putty登录 (为了安全起见,不给过多的权限) 1.创建xiao用户 [root@localh ...
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!
利用SecureCRT上传.下载文件(使用sz与rz命令),超实用! 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39623169 借 ...
- Linux--用SecureCRT来上传和下载文件
SecureCRT下的文件传输协议有以下几种:ASCII.Xmodem.Ymodem.Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. Xmodem:这种古老的传输协议速度较慢,但由 ...
- js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中
ajaxfileupload.js jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId ...
- SecureCRT来上传和下载文件
引用:https://www.cnblogs.com/zhengyihan1216/p/6260667.html Linux--用SecureCRT来上传和下载文件 SecureCRT下的文件传输协议 ...
随机推荐
- Sql server 表表达式
1.表表达式概述 (1)表表达式(table expression) 是一个命名的查询表达式.代表一个有效的关系表 (2)在DML 中,使用表表达式和使用其他表非常类似 (3)sqlserver 支持 ...
- java课程之团队开发冲刺阶段2.7
昨日总结: 1.完整实现课前闹钟提醒功能 遇到的困难: 1.没有遇到大的问题,细节地方没有处理好出现了一下小的情况 今天的任务: 1.实现对课程查询的完整实现 当日总结: 1.以前是使用二级联动下拉框 ...
- 吴裕雄--天生自然TensorFlow2教程:合并与分割
import tensorflow as tf # 6个班级的学生分数情况 a = tf.ones([4, 35, 8]) b = tf.ones([2, 35, 8]) c = tf.concat( ...
- 在Linux下 MySQL错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 解决办法【很管用】
一般这个错误是由密码错误引起,解决的办法自然就是重置密码. 假设我们使用的是root账户. 1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:wi ...
- js获取浏览器窗口大小
摘抄:https://blog.csdn.net/qq_27628085/article/details/81947478 常用: JS 获取浏览器窗口大小 // 获取窗口宽度 if ...
- 中后缀表达式/洛谷P1175 表达式的转换
P1175 表达式的转换 思路:先用栈转成中缀表达式,再用栈进行计算.要输出过程,因此计算一次输出一次,但是栈没有迭代器,不好用,换成vector(可以pop_back).虽然表达式求值也可以这么做, ...
- Arduino IIC 主从设备连接通信
目的: 实现Arduino主从设备之间的互相IIC通信,掌握IIC通信协议的使用方法. 器材: Arduino UNO R3 一块 Arduino Nano 三块 面包板 导线 3K ...
- class选择器,外部样式表,选择器优先级
class选择器: 先在相应标签中设置一个class属性,如class=“class名”.class名{ ……css样式}注:class名以英文字母开头,可以多个标签重复使用.优先级:标签名选择器 & ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 索引
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是 ...