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创建文件夹、创建文件、上传文件,下载文件的更多相关文章

  1. 使用paramiko模块远程登录并上传或下载文件

    1.paramiko安装 1)安装PyCrypto2.6 for Python 2.7 64bit.地址:http://www.voidspace.org.uk/python/modules.shtm ...

  2. ftp上传或下载文件工具类

    FtpTransferUtil.java工具类,向ftp上传或下载文件: package utils; import java.io.File; import java.io.FileOutputSt ...

  3. 如何在Linux中使用sFTP上传或下载文件与文件夹

    如何在Linux中使用sFTP上传或下载文件与文件夹 sFTP(安全文件传输程序)是一种安全的交互式文件传输程序,其工作方式与 FTP(文件传输协议)类似. 然而,sFTP 比 FTP 更安全;它通过 ...

  4. Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)

            一.接收参数(postman发送) 1.form表单 @RequestParam("name") String name 会把传递过来的Form表单中的name对应 ...

  5. Linux学习笔记:使用ftp命令上传和下载文件

    Linux中如何使用ftp命令,包括如何连接ftp服务器,上传or下载文件以及创建文件夹.虽然现在有很多ftp桌面应用(例如:FlashFXP),但是在服务器.SSH.远程会话中掌握命令行ftp的使用 ...

  6. 11、只允许在主目录下上传和下载文件,不允许用putty登录

    创建用户xiao,   使其只允许在用户主目录 (/var/www/html)下上传和下载文件,不允许用putty登录 (为了安全起见,不给过多的权限) 1.创建xiao用户 [root@localh ...

  7. 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!

    利用SecureCRT上传.下载文件(使用sz与rz命令),超实用! 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39623169 借 ...

  8. Linux--用SecureCRT来上传和下载文件

    SecureCRT下的文件传输协议有以下几种:ASCII.Xmodem.Ymodem.Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. Xmodem:这种古老的传输协议速度较慢,但由 ...

  9. js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中

    ajaxfileupload.js jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId ...

  10. SecureCRT来上传和下载文件

    引用:https://www.cnblogs.com/zhengyihan1216/p/6260667.html Linux--用SecureCRT来上传和下载文件 SecureCRT下的文件传输协议 ...

随机推荐

  1. centos 制作指定需求命令的YUM源

    场景: 没有YUM源,但是需要安装一些用到的命令,如vim,telnet等少量命令,不想YUM源太大,满足需求即可.于是制作一个仅需要满足要求的yum源 步骤一: 联网环境下安装createreo命令 ...

  2. 自定义alert

    参考:https://www.cnblogs.com/st-leslie/articles/5279864.html 把window.alert=function(){}指向新的方法,即相当于重写 w ...

  3. (排序)P1177 【模板】快速排序

    题解: 这道题用传统快排(如下所示)的结果就是最后三个点TLE: 如果永远取第一个元素作为枢轴的话,在数组已经有序的情况下每次划分都将得到最坏的结果,时间复杂度退化为O(n^2).因为其中一个子序列每 ...

  4. 在GitHub上上传自己的项目

    1.注册一个自己的GitHub账号 2.登录,开始创建一个新的项目,填写项目名称,描述 创建完成之后,跳转到下面的页面,下面红框中的网址要记住,在后面上传代码的时候需要使用 3.下载最新版本的Git  ...

  5. HttpServletRequest 的常用属性说明

    HttpServletRequest 的常用属性总是被窝遗忘,人老了记性就不好.所以做个笔记,方便以后查看. 测试地址:http://127.0.0.1:8080/Test/test getConte ...

  6. mysql 杂

    mysql> show create table  table_name|view_name \G; 可以查看是视图还是表      \G 放到sql语句后,可以使每个字段打印到单独的行 1 I ...

  7. PHP 的变量类型,变量检测

    1.PHP的变量类型: 整型       浮点型 字符串 布尔型 数组 对象 null 资源类型 一个变量就是一个盒子,类型可以看做盒子的标签,变量的值就是盒子里的内容 null 是没有类型的空盒子, ...

  8. The full stack trace of the root cause is available in the Apache Tomcat/8.0.8 logs.

    这个问题是版本冲突的问题 1.调低jdk 版本,不能让jdk版本太高,至少不能比tomcat高,要不然就会有这个错误. 2.如果看过我这篇博客的人(https://www.cnblogs.com/CH ...

  9. ES6 之 第七种数据类型Symbol

    概述 为了减少对象的属性名冲突,ES6引入新的原始数据类型Symbol,JS的第七种数据类型. Symbol 能够保证每个属性的名字都是独一无二,这样就能从根本上防止属性名冲突. Symbol 值能够 ...

  10. 1.4CAD2017绘图基础

    1.新建(ctrl+n) 命令:new 回车——默认样板(acadiso.dwt) 2.打开(ctr+o) 3.保存(ctrl+S) 4.鼠标的应用: 左键:点击拖选等: 中间滚轮:a.滚动,放大缩小 ...