java 文件保存到本地
private void savePic(InputStream inputStream, String fileName) {
OutputStream os = null;
try {
String path = "D:\\testFile\\";
// 2、保存到临时文件
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流保存到本地文件
File tempFile = new File(path);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完毕,关闭所有链接
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件保存方法.
附:
package com.ebways.web.upload.controller; import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import com.ebways.web.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ebways.web.upload.url.UploadURL;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Controller
@RequestMapping(value = UploadURL.MODE_NAME)
public class UploadController extends BaseController { @RequestMapping(value = UploadURL.IMAGE_UPLOAD)
@ResponseBody
public String uploadFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, Exception {
// 参数列表
Map<String, Object> map = new HashMap<>();
map.put("file", file);
savePic(file.getInputStream(), file.getOriginalFilename()); //请求接口
String apiReturnStr = "";//apiHttpRequest(map, API_HOST_URL + "/image/upload"); return apiReturnStr;
} private void savePic(InputStream inputStream, String fileName) { OutputStream os = null;
try {
String path = "D:\\testFile\\";
// 2、保存到临时文件
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流保存到本地文件 File tempFile = new File(path);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
} } catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完毕,关闭所有链接
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* <p class="detail">
* 功能:公共Action
* </p>
*
* @date 2016年9月8日
* @author wangsheng
*/
private String allowSuffix = "jpg,png,gif,jpeg";//允许文件格式
private long allowSize = 2L;//允许文件大小
private String fileName;
private String[] fileNames; public String getAllowSuffix() {
return allowSuffix;
} public void setAllowSuffix(String allowSuffix) {
this.allowSuffix = allowSuffix;
} public long getAllowSize() {
return allowSize * 1024 * 1024;
} public void setAllowSize(long allowSize) {
this.allowSize = allowSize;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public String[] getFileNames() {
return fileNames;
} public void setFileNames(String[] fileNames) {
this.fileNames = fileNames;
} /**
* <p class="detail">
* 功能:重新命名文件
* </p>
*
* @return
* @author wangsheng
* @date 2016年9月8日
*/
private String getFileNameNew() {
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return fmt.format(new Date());
} /**
* <p class="detail">
* 功能:文件上传
* </p>
*
* @param files
* @param destDir
* @throws Exception
* @author wangsheng
* @date 2014年9月25日
*/
public void uploads(MultipartFile[] files, String destDir, HttpServletRequest request) throws Exception {
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
try {
fileNames = new String[files.length];
int index = 0;
for (MultipartFile file : files) {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
int length = getAllowSuffix().indexOf(suffix);
if (length == -1) {
throw new Exception("请上传允许格式的文件");
}
if (file.getSize() > getAllowSize()) {
throw new Exception("您上传的文件大小已经超出范围");
}
String realPath = request.getSession().getServletContext().getRealPath("/");
File destFile = new File(realPath + destDir);
if (!destFile.exists()) {
destFile.mkdirs();
}
String fileNameNew = getFileNameNew() + "." + suffix;//
File f = new File(destFile.getAbsoluteFile() + "\\" + fileNameNew);
file.transferTo(f);
f.createNewFile();
fileNames[index++] = basePath + destDir + fileNameNew;
}
} catch (Exception e) {
throw e;
}
} /**
* <p class="detail">
* 功能:文件上传
* </p>
*
* @param file
* @param destDir
* @throws Exception
* @author wangsheng
* @date 2016年9月8日
*/
public void upload(MultipartFile file, String destDir, HttpServletRequest request) throws Exception {
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
try {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
int length = getAllowSuffix().indexOf(suffix);
if (length == -1) {
throw new Exception("请上传允许格式的文件");
}
if (file.getSize() > getAllowSize()) {
throw new Exception("您上传的文件大小已经超出范围");
} String realPath = request.getSession().getServletContext().getRealPath("/");
File destFile = new File(realPath + destDir);
if (!destFile.exists()) {
destFile.mkdirs();
}
String fileNameNew = getFileNameNew() + "." + suffix;
File f = new File(destFile.getAbsoluteFile() + "/" + fileNameNew);
file.transferTo(f);
f.createNewFile();
fileName = basePath + destDir + fileNameNew;
} catch (Exception e) {
throw e;
}
} }
java 文件保存到本地的更多相关文章
- php 下载保存文件保存到本地的两种方法
第一种: 1 <? ?> 或 <?php //下载文件保存到本地//www.jbxue.comfunction downfile($fileurl){ob_start(); $fil ...
- C# 中从网络上下载文件保存到本地文件
下面是C#中常用的从Internet上下载文件保存到本地的一些方法,没有太多的技巧. 1.通过 WebClient 类下载文件 WebClient webClient = new WebClien ...
- Java读取oracle数据库中blob字段数据文件保存到本地文件(转载)
转自:https://www.cnblogs.com/forever2698/p/4747349.html package com.bo.test; import java.io.FileOutput ...
- 如何将S/4HANA系统存储的图片文件用Java程序保存到本地
我在S/4HANA的事务码MM02里为Material维护图片文件作为附件: 通过如下简单的ABAP代码即可将图片文件的二进制内容读取出来: REPORT zgos_api. DATA ls_appl ...
- java文件保存至服务器
import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java ...
- 【转】php 下载保存文件保存到本地的两种实现方法
来源:http://www.jb51.net/article/40485.htm 第一种: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php function d ...
- php 下载保存文件保存到本地的两种实现方法
这里的下载,指的是 弹出下载提示框. 第一种: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php function downfile() { $filename ...
- BBS(第三天) 如何吧用户上传的图片文件保存到本地
1. 将用户上传的所有静态文件统一管理 -- settings.py -- MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 2. 服务器会对外公开一下服务器静 ...
- C#如何实现下载文件保存到本地上面去
public void btnTemplate_Click(object sender, EventArgs e) { string strResult = string.Empty; string ...
随机推荐
- 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
原因: vs2010默认的是4.0框架,4.0的框架是独立的CLR,和2.0的不同,如果想运行4.0的网站,需要用aspnet_regiis注册4.0框架,然后用4.0的Class池,就可以运行4.0 ...
- CentOS升级openssl
才设置了http2,结果蓝狗说我网站不安全,检测一下发现openssl有漏洞,于是准备升级一下openssl 检测网站: www.ssllabs.com/ssltest/analyze.html # ...
- 使用Mysql Workbench 画E-R图
MySQL Workbench 是一款专为MySQL设计的ER/数据库建模工具.你可以用MySQL Workbench设计和创建新的数据库图示,建立数据库文档,以及进行复杂的MySQL 迁移.这里介绍 ...
- cx_Oracle摘记
由于想使用python操作oracle所以查看了cx_Oracle的官方文档,同时也查看了twisted中cx_Oracle的使用.下面是摘自文档中一些我认为有用的内容 cx_Oracle is a ...
- VR、AR、MR的区别
VR.AR.MR定义: 什么是虚拟现实? 虚拟现实(Virtual Reality,简称VR,又译作灵境.幻真)是近年来出现的高新技术,也称灵境技术或人工环境.虚拟现实是利用电脑模拟产生一个三维空间的 ...
- vs中使用beyondcompare比较
开启 Visual Studio 的 [工具] /[选项] / [源代码管理] /[Visual Studio Team Foundation],并开启「配置用户工具」 如下图单击「添加」按钮 接着 ...
- Django知识点整理
什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. web应用 访 ...
- ReactNative 告别CodePush,自建热更新版本升级环境
微软的CodePush热更新非常难用大家都知道,速度跟被墙了没什么区别. 另外一方面,我们不希望把代码放到别人的服务器.自己写接口更新总归感觉安全一点. so,就来自己搞个React-Native A ...
- Cookie和Session的区别
前言 HTTP是一种无状态的协议,为了分辨链接是谁发起的,就需要我们自己去解决这个问题.不然有些情况下即使是同一个网站我们每打开一个页面也都要登录一下.而Session和Cookie就是为解决这个问题 ...
- 2016福州大学软件工程第五、六次团队作业-Alpha阶段成绩汇总
1.本次作业成绩统计结果: 本次Alpha阶段团队作业公布如下: 表格说明: PE:贡献百分比 YS:演示评分(满分15分) BK:博客评分(满分15分) SH:事后诸葛亮环节(满分5分) P:个人分 ...