图片上传及获取

图片上传

使用MultipartFile上传图片到tomcat的webapps目录pic下面

public JsonResult saveImageFile(@RequestParam(value = "imageFile", required = false) MultipartFile imageFile, @RequestParam(value = "fileName", required = false) String fileName, HttpServletRequest request) {
    result = new JsonResult();
    try {
        //image输出地址
        //String picPath = "/data/sdwan/apache-tomcat-8.5.32/webapps/pic/";
       // String picPath = request.getSession().getServletContext().getRealPath("upload/");//本地项目路径
        //String photoFileName = imageFile.getOriginalFilename();
        File file=new File("");
        String picPath=file.getAbsolutePath().replace("bin","webapps\\pic\\");
        File newFile = new File(picPath+fileName);
        // 文件存在
        if (!newFile.exists()) {
            logger.info( picPath+"  targetFile is not exist");
            // 新建文件目录
            newFile.mkdirs();
        }
        imageFile.transferTo(newFile);  //保存文件
        result.setRetCode("200");
        result.setRetMessage("图片上传成功!");

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result.setRetCode("400");
        result.setRetMessage("图片上传失败!");
    } catch (IOException e) {
        e.printStackTrace();
        result.setRetCode("400");
        result.setRetMessage("图片上传失败!");
    }

    return result;
}

 

图片的获取:http://localhost:8080/sdwan/pic/1560160595232kbq0s85qqta.jpg

在tomcat设置url虚拟路径映射:server.xml

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
      <Context docBase="D:\Tomcat\webapps\pic" path="/sdwan/pic" reloadable="true"/>
    
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"

              pattern="%h %l %u %t &quot;%r&quot; %s %b" />                     

      </Host>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值