最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java实现文件保存到本地的方法
时间:2017-03-09 编辑:简简单单 来源:一聚教程网
代码如下 | 复制代码 |
privatevoidsavePic(InputStream inputStream, String fileName) {
OutputStream os =null; try{ String path ="D:\\testFile\\"; // 2、保存到临时文件 // 1K的数据缓冲 byte[] bs =newbyte[1024]; // 读取到的数据长度 intlen; // 输出的文件流保存到本地文件
File tempFile =newFile(path); if(!tempFile.exists()) { tempFile.mkdirs(); } os =newFileOutputStream(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(); } } } |
文件保存方法.
附:
代码如下 | 复制代码 |
packagecom.ebways.web.upload.controller;
importjava.io.*; importjava.text.SimpleDateFormat; importjava.util.Date; importjava.util.HashMap; importjava.util.Map;
importcom.ebways.web.base.BaseController; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestParam; importorg.springframework.web.bind.annotation.ResponseBody; importcom.ebways.web.upload.url.UploadURL; importorg.springframework.web.multipart.MultipartFile;
importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse;
@Controller @RequestMapping(value = UploadURL.MODE_NAME) publicclassUploadControllerextendsBaseController {
@RequestMapping(value = UploadURL.IMAGE_UPLOAD) @ResponseBody publicString uploadFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response)throwsIllegalArgumentException, Exception { // 参数列表 Map map.put("file", file); savePic(file.getInputStream(), file.getOriginalFilename());
//请求接口 String apiReturnStr ="";//apiHttpRequest(map, API_HOST_URL + "/image/upload");
returnapiReturnStr; }
privatevoidsavePic(InputStream inputStream, String fileName) {
OutputStream os =null; try{ String path ="D:\\testFile\\"; // 2、保存到临时文件 // 1K的数据缓冲 byte[] bs =newbyte[1024]; // 读取到的数据长度 intlen; // 输出的文件流保存到本地文件
File tempFile =newFile(path); if(!tempFile.exists()) { tempFile.mkdirs(); } os =newFileOutputStream(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(); } } }
/** * * 功能:公共Action * * * @date 2016年9月8日 * @author wangsheng */ privateString allowSuffix ="jpg,png,gif,jpeg";//允许文件格式 privatelongallowSize = 2L;//允许文件大小 privateString fileName; privateString[] fileNames;
publicString getAllowSuffix() { returnallowSuffix; }
publicvoidsetAllowSuffix(String allowSuffix) { this.allowSuffix = allowSuffix; }
publiclonggetAllowSize() { returnallowSize *1024*1024; }
publicvoidsetAllowSize(longallowSize) { this.allowSize = allowSize; }
publicString getFileName() { returnfileName; }
publicvoidsetFileName(String fileName) { this.fileName = fileName; }
publicString[] getFileNames() { returnfileNames; }
publicvoidsetFileNames(String[] fileNames) { this.fileNames = fileNames; }
/** * * 功能:重新命名文件 * * * @return * @author wangsheng * @date 2016年9月8日 */ privateString getFileNameNew() { SimpleDateFormat fmt =newSimpleDateFormat("yyyyMMddHHmmssSSS"); returnfmt.format(newDate()); }
/** * * 功能:文件上传 * * * @param files * @param destDir * @throws Exception * @author wangsheng * @date 2014年9月25日 */ publicvoiduploads(MultipartFile[] files, String destDir, HttpServletRequest request)throwsException { String path = request.getContextPath(); String basePath = request.getScheme() +"://"+ request.getServerName() +":"+ request.getServerPort() + path; try{ fileNames =newString[files.length]; intindex =0; for(MultipartFile file : files) { String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") +1); intlength = getAllowSuffix().indexOf(suffix); if(length == -1) { thrownewException("请上传允许格式的文件"); } if(file.getSize() > getAllowSize()) { thrownewException("您上传的文件大小已经超出范围"); } String realPath = request.getSession().getServletContext().getRealPath("/"); File destFile =newFile(realPath + destDir); if(!destFile.exists()) { destFile.mkdirs(); } String fileNameNew = getFileNameNew() +"."+ suffix;// File f =newFile(destFile.getAbsoluteFile() +"\\"+ fileNameNew); file.transferTo(f); f.createNewFile(); fileNames[index++] = basePath + destDir + fileNameNew; } }catch(Exception e) { throwe; } }
/** *功能:文件上传 * * @param file * @param destDir * @throws Exception * @author wangsheng * @date 2016年9月8日 */ publicvoidupload(MultipartFile file, String destDir, HttpServletRequest request)throwsException { String path = request.getContextPath(); String basePath = request.getScheme() +"://"+ request.getServerName() +":"+ request.getServerPort() + path; try{ String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") +1); intlength = getAllowSuffix().indexOf(suffix); if(length == -1) { thrownewException("请上传允许格式的文件"); } if(file.getSize() > getAllowSize()) { thrownewException("您上传的文件大小已经超出范围"); }
String realPath = request.getSession().getServletContext().getRealPath("/"); File destFile =newFile(realPath + destDir); if(!destFile.exists()) { destFile.mkdirs(); } String fileNameNew = getFileNameNew() +"."+ suffix; File f =newFile(destFile.getAbsoluteFile() +"/"+ fileNameNew); file.transferTo(f); f.createNewFile(); fileName = basePath + destDir + fileNameNew; }catch(Exception e) { throwe; } }
} |
相关文章
- AI文件没保存怎么找回 AI未保存文件的找回方法 02-04
- 拍大师视频保存在哪里 拍大师视频文件存在哪 01-16
- 如何把ping结果保存到指定文件 12-28
- fso 文件保存 08-28
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24