java后端接收手机端图片_前台用weui上传照片,java后台怎么接收图片

package io.nutz.nutzsite.module.open.file;

import io.nutz.nutzsite.common.base.Result;

import io.nutz.nutzsite.common.utils.UpLoadUtil;

import io.nutz.nutzsite.module.sys.services.ImageService;

import org.nutz.ioc.loader.annotation.Inject;

import org.nutz.ioc.loader.annotation.IocBean;

import org.nutz.lang.util.NutMap;

import org.nutz.log.Log;

import org.nutz.log.Logs;

import org.nutz.mvc.annotation.*;

import org.nutz.mvc.impl.AdaptorErrorContext;

import org.nutz.mvc.upload.TempFile;

import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

/**

* @Author: Haimming

* @Date: 2019-05-08 13:42

* @Version 1.0

*/

@IocBean

@At("/open/file")

public class UploadController {

private static final Log log = Logs.get();

@Inject

private ImageService imageService;

@At("/get/?")

@Ok("raw")

public void get(String id,HttpServletRequest req, HttpServletResponse resp) {

String path = imageService.get(id);

File file = new File(path);

// 取得文件名。

String filename = file.getName();

// 取得文件的后缀名。

String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 以流的形式下载文件。

try (InputStream fis = new BufferedInputStream(new FileInputStream(path))){

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

// 清空response

resp.reset();

// 设置response的Header

resp.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));

resp.addHeader("Content-Length", "" + file.length());

OutputStream toClient = new BufferedOutputStream(resp.getOutputStream());

resp.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

toClient.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

@At

@Ok("json")

@POST

@AdaptBy(type = UploadAdaptor.class)

public Object upload(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {

try {

if (err != null && err.getAdaptorErr() != null) {

return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");

} else if (tf == null) {

return Result.error("空文件");

} else {

String url = UpLoadUtil.upLoadFileSysConfigPath(tf,"tmp");

String u = req.getServletContext().getContextPath();

return Result.success("上传成功", u + url );

}

} catch (Exception e) {

e.printStackTrace();

return Result.error("系统错误");

} catch (Throwable e) {

return Result.error("图片格式错误");

}

}

}

文件上传需要加注解

可以用POSTMAN 先测试测试 看你写的方法

@AdaptBy(type = UploadAdaptor.class)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值