微信小程序上传图片springboot接口带判断条件

@Value("${jc.tmp.path}")    取出在application中配置的图片存储路径

privateStringlocalImgPath;  


/**

* InputStream,String,File相互转化

*@paramins

*@paramfile

*/

private voidinputstreamtofile(InputStream ins, File file)

{

OutputStream os =null;

try

{

os =newFileOutputStream(file);

intbytesRead =0;

byte[] buffer =new byte[8192];

while((bytesRead = ins.read(buffer,0,8192)) != -1)

{

os.write(buffer,0, bytesRead);

}

}catch(FileNotFoundException e)

{

e.printStackTrace();

}catch(IOException e)

{

e.printStackTrace();

}finally

{

try

{

if(os !=null)

{

os.close();

}

if(ins !=null)

{

ins.close();

}

}catch(IOException e)

{

e.printStackTrace();

}

}

}

//处理文件上传

@RequestMapping(value ="/uploadimg", method = RequestMethod.POST, consumes ="multipart/form-data")

@ResponseBody

publicString uploadImg(HttpServletRequest request)throwsException

{

ApplicationPart image = (ApplicationPart) request.getPart("file");

InputStream in;

String a ="error";

//1.在缓存里有

if(image ==null)

{

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

MultipartFile mfile = multipartRequest.getFile("file");

in = mfile.getInputStream();

String webPath = request.getServletContext().getRealPath("/");

System.out.println("webPath*****************"+ webPath);

String path =localImgPath+ mfile.getOriginalFilename();

System.out.println("save path:**************"+ path);

inputstreamtofile(in,newFile(path));

a = mfile.getOriginalFilename();

}else   //2.传上来的图片直接在流里有

{

in = image.getInputStream();

String webPath = request.getServletContext().getRealPath("/");

System.out.println("webPath*****************"+ webPath);

String path =localImgPath+ image.getSubmittedFileName();

System.out.println("save path:**************"+ path);

inputstreamtofile(in,newFile(path));

a = image.getSubmittedFileName();

}

//String[] names=a.split("\\.");

returna;

}

在application中定义 文件取出或保存的路径

jc.tmp.path=/home/cus/tmp/

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一:在idea项目中创建IndexController, //处理文件上传 @RequestMapping(val...
    丁忠康阅读 9,288评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,904评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,594评论 18 399
  • 一、流的概念和作用。 流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象。即数据在两设备之间的传输...
    布鲁斯不吐丝阅读 13,427评论 2 95
  • 《可否这样相爱》完整目录 一帘幽思,浓抹茫茫。红烛泣,情堪伤,醉依罗衾凉。梦觅寄客乡,何处枕黄梁。晨钟悠,暮鼓扬,...
    晚成寄语阅读 3,641评论 4 18

友情链接更多精彩内容