java实现文件保存到本地的方法
程序员文章站
2024-03-07 12:29:39
本篇介绍了java实现文件保存到本地的方法,具体代码如下:
private void savepic(inputstream inputstream, str...
本篇介绍了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; } } /** *功能:文件上传 * * @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; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Android打开相机和相册实例代码