java实现⽂件保存到本地的⽅法
本篇介绍了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 (!ists()) {
tempFile.mkdirs();
}
os = new Path() + File.separator + fileName);
// 开始读取
while ((len = ad(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.ller;
import java.io.*;
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);
InputStream(), OriginalFilename());
//请求接⼝
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 (!ists()) {
tempFile.mkdirs();
}
os = new Path() + File.separator + fileName);
// 开始读取
while ((len = ad(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">
getsavefilename* 功能:重新命名⽂件
* </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 = ContextPath();
String basePath = Scheme() + "://" + ServerName() + ":" + ServerPort() + path; try {
fileNames = new String[files.length];
int index = 0;
for (MultipartFile file : files) {
String suffix = OriginalFilename().OriginalFilename().lastIndexOf(".") + 1);
int length = getAllowSuffix().indexOf(suffix);
if (length == -1) {
throw new Exception("请上传允许格式的⽂件");
}
if (Size() > getAllowSize()) {
throw new Exception("您上传的⽂件⼤⼩已经超出范围");
}
String realPath = Session().getServletContext().getRealPath("/");
File destFile = new File(realPath + destDir);
if (!ists()) {
destFile.mkdirs();
}
String fileNameNew = getFileNameNew() + "." + suffix;//
File f = new AbsoluteFile() + "\\" + fileNameNew);
fileNames[index++] = basePath + destDir + fileNameNew;
}
} catch (Exception e) {
throw e;
}
}
/**
*功能:⽂件上传
*
* @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 = ContextPath();
String basePath = Scheme() + "://" + ServerName() + ":" + ServerPort() + path; try {
String suffix = OriginalFilename().OriginalFilename().lastIndexOf(".") + 1);
int length = getAllowSuffix().indexOf(suffix);
if (length == -1) {
throw new Exception("请上传允许格式的⽂件");
}
if (Size() > getAllowSize()) {
throw new Exception("您上传的⽂件⼤⼩已经超出范围");
}
String realPath = Session().getServletContext().getRealPath("/");
File destFile = new File(realPath + destDir);
if (!ists()) {
destFile.mkdirs();
}
String fileNameNew = getFileNameNew() + "." + suffix;
File f = new AbsoluteFile() + "/" + fileNameNew);
fileName = basePath + destDir + fileNameNew;
} catch (Exception e) {
throw e;
}
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论