springmvc如何接收前端传过来的图片
1个回答
2018-07-05 · 国内最优秀java资源共享平台
关注
展开全部
案例演示
1. jsp
<form action="/eshop/upload/uploadimg" enctype="multipart/form-data" method="post">
上传图片:<input type="file" name="file" value="选择图片"><br>
<input type="submit" value="上传">
</form>
2.Controller
@Controller
@RequestMapping("upload")
public class UploadController {
@RequestMapping("uploadimg")
public String uploadimg(MultipartFile file) throws Exception{
String filename = file.getOriginalFilename();
System.out.println(filename);
//写入本地磁盘
InputStream is = file.getInputStream();
byte[] bs = new byte[1024];
int len;
OutputStream os = new FileOutputStream(new File("D:/temp/" + filename));
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
return "ok";
}
}
1. jsp
<form action="/eshop/upload/uploadimg" enctype="multipart/form-data" method="post">
上传图片:<input type="file" name="file" value="选择图片"><br>
<input type="submit" value="上传">
</form>
2.Controller
@Controller
@RequestMapping("upload")
public class UploadController {
@RequestMapping("uploadimg")
public String uploadimg(MultipartFile file) throws Exception{
String filename = file.getOriginalFilename();
System.out.println(filename);
//写入本地磁盘
InputStream is = file.getInputStream();
byte[] bs = new byte[1024];
int len;
OutputStream os = new FileOutputStream(new File("D:/temp/" + filename));
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
return "ok";
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |