没有合适的资源?快使用搜索试试~ 我知道了~
java实现文件上传、下载、图片预览
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
6 下载量 115 浏览量
2020-08-25
04:48:16
上传
评论 2
收藏 33KB PDF 举报
温馨提示
试读
2页
主要介绍了java实现文件上传、下载、图片预览,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
资源详情
资源评论
资源推荐
java实现文件上传、下载、图片预览实现文件上传、下载、图片预览
主要介绍了java实现文件上传、下载、图片预览,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一
定的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了java实现文件上传、下载、图片预览,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
考学习价值,需要的朋友可以参考下
多文件保存到本地:
@ResponseBody
@RequestMapping(value = "/uploadApp",produces = { "application/json;charset=UTF-8" },method= RequestMethod.POST)
public String uploadApp( HttpServletRequest request,@RequestParam("file") MultipartFile[] file) throws IOException {
try {
if(file.length > 0) {
String name = file[0].getOriginalFilename().split(";")[0];
String fileUrlName = CommonEnum.FILEPATH+"/"+name;
for (int i = 0; i < file.length; i++) {
FileUtils.copyInputStreamToFile(file[i].getInputStream(), new File(fileUrlName, file[i].getOriginalFilename().split(";")[1]));
}
return "success";
}else{
return "null";
}
}catch (Exception e){
e.printStackTrace();
return "error";
}
}
下载文件:
@RequestMapping(value = "/download", method = RequestMethod.GET)
@ResponseBody
public void download(@RequestParam Map<String, Object> data, HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException {
String time = DateUtil.formatFromDate("yyyyMMddHHmmss", new Date());
List<Map<String, Object>> urllist = companyService.findByIMG(data);
String path = (String) urllist.get(0).get("imgurl");
String docx = StringUtils.substringAfterLast(path, ".");
String fileName = time+"."+docx; // 文件的默认保存名
InputStream inStream = new FileInputStream(path);// 文件的存放路径
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取图片
*/
@RequestMapping(value = "/iomoreimgcom", produces = {
"application/json;charset=UTF-8" }, method = RequestMethod.GET)
@ResponseBody
public synchronized void iomoreimgcom(HttpServletRequest request, HttpServletResponse response) throws Exception {
String url = request.getParameter("url");
File file = new File(url);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
response.setHeader("Content-Type", "image/jpeg");
byte b[] = new byte[1024];
int read;
try {
while ((read = bis.read(b)) != -1) {
bos.write(b, 0, read);
}
//request.getRequestDispatcher("/components/hazard/yscchird.html").forward(request, response);
} catch (Exception e) {
// TODO: handle exception
} finally {
if (bos != null) {
weixin_38686677
- 粉丝: 2
- 资源: 924
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0