java实现文件上传、下载(包含下载网络图片上传到服务器上)
2018-09-06 17:24
2261 查看
拿去直接用就好:
1、文件上传
package com.myproject.aiplatform.core.utils; import java.io.BufferedInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.io.FileUtils; import org.springframework.web.multipart.MultipartFile; /** * 上传文件 * @version */ public class FileUpload { /**上传文件 * @param file //文件对象 * @param filePath //上传路径 * @param fileName //文件名 * @return 文件名 */ public static String fileUp(MultipartFile file, String filePath, String fileName){ String extName = ""; // 扩展名格式: try { if (file.getOriginalFilename().lastIndexOf(".") >= 0){ extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); } copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", ""); } catch (IOException e) { System.out.println(e); } return fileName+extName; } /** * 写文件到当前目录的upload目录中 * @param in * @param fileName * @throws IOException */ public static String copyFile(InputStream in, String dir, String realName) throws IOException { File file = mkdirsmy(dir,realName); FileUtils.copyInputStreamToFile(in, file); in.close(); return realName; } /**判断路径是否存在,否:创建此路径 * @param dir 文件路径 * @param realName 文件名 * @throws IOException */ public static File mkdirsmy(String dir, String realName) throws IOException{ File file = new File(dir, realName); if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } return file; } /**下载网络图片上传到服务器上 * @param httpUrl 图片网络地址 * @param filePath 图片保存路径 * @param myFileName 图片文件名(null时用网络图片原名) * @return 返回图片名称 */ public static String getHtmlPicture(String httpUrl, String filePath , String myFileName) { URL url; //定义URL对象url BufferedInputStream in; //定义输入字节缓冲流对象in FileOutputStream file; //定义文件输出流对象file try { String fileName = null == myFileName?httpUrl.substring(httpUrl.lastIndexOf("/")).replace("/", ""):myFileName; //图片文件名(null时用网络图片原名) url = new URL(httpUrl); //初始化url对象 in = new BufferedInputStream(url.openStream()); //初始化in对象,也就是获得url字节流 //file = new FileOutputStream(new File(filePath +"\\"+ fileName)); file = new FileOutputStream(mkdirsmy(filePath,fileName)); int t; while ((t = in.read()) != -1) { file.write(t); } file.close(); in.close(); return fileName; } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
2、文件下载
package com.myproject.aiplatform.core.utils; import java.io.BufferedOutputStream; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; /** * 下载文件 * @version */ public class FileDownload { /** * @param response * @param filePath //文件完整路径(包括文件名和扩展名) * @param fileName //下载后看到的文件名 * @return 文件名 */ public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception{ byte[] data = FileUtil.toByteArray2(filePath); fileName = URLEncoder.encode(fileName, "UTF-8"); response.reset(); response.setHeader("Content-Disposition", "attachment; filename=" + fileName ); response.addHeader("Content-Length", "" + data.length); response.setContentType("application/octet-stream;charset=UTF-8"); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); outputStream.write(data); outputStream.flush(); outputStream.close(); response.flushBuffer(); } }阅读更多
相关文章推荐
- java操作ftp实现文件的上传下载(适用于图片文档服务器)
- Java实现ftp上传下载、删除文件及在ftp服务器上传文件夹的方法
- JAVA实现FTP服务器文件的上传,下载,删除功能
- java实现将文件上传到本地的ftp服务器中(layui前端框架form表单数据与图片一起提交)
- Java FTP 实现跨服务器文件上传下载
- Java实现FTP服务器的上传、下载、删除、查看文件列表
- Java 小程序 实现网络下载图片到制定文件夹,压缩和删除文件
- Java学习日记8:文件上传工具类的实现和服务器给客户端发送图片
- Java连接FTP服务器并且实现对其文件的上传和下载
- Java连接FTP服务器并且实现对其文件的上传和下载
- Python实现简易文件服务器 包含上传下载删除新建目录功能
- Java FTP 实现跨服务器文件上传下载
- java+jsp代码实现从FTP服务器上传下载文件
- java实现文件上传下载至ftp服务器
- Java实现上传文件图片到指定服务器目录
- Java连接FTP服务器并且实现对其文件的上传和下载
- Java连接FTP服务器并且实现对其文件的上传和下载
- Java Springboot结合FastDFS实现文件上传以及根据图片url将图片上传至图片服务器
- Java代码实现FTP服务器上传文件与下载文件