java实现简单图片上传下载功能

时间:2021-05-19

本文实例为大家分享了java实现简单图片上传下载的具体代码,供大家参考,具体内容如下

1.首先在上传图片界面:将form表单的enctype改为:multipart/form-data

2.定义一个实体类用来将存放图片存放的路径存入到mysql中private String imgpath;

3.在spring容器中注入处理图片的解析器

<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置默认编码 --> <property name="defaultEncoding" value="utf-8"></property> <!-- 上传图片最大大小5M--> <property name="maxUploadSize" value="5242440"></property> </bean>

4.在controller层接收的时候需要用 @RequestParam("file") CommonsMultipartFile file来接收,如果是多个图片就是@RequestParam("file") CommonsMultipartFile[] files来接收

5.通过工具类处理返回要存入实体类的图片的路径

public class FileUpUtil { /** * 上传多个文件或图片上传在项目路径下的img文件夹在 * !!!!!!重新部署项目实效,因为文件夹被删除 * @param files * @param request * @return */ public static List<String> upfiles(CommonsMultipartFile files[],HttpServletRequest request){ List<String> list = new ArrayList<String>(); // 获得项目的路径 ServletContext sc = request.getSession().getServletContext(); // 上传位置 String path = sc.getRealPath("/img") + File.separatorChar; // 设定文件保存的目录 File f = new File(path); if (!f.exists()) f.mkdirs(); for (int i = 0; i < files.length; i++) { // 获得原始文件名 String fileName = files[i].getOriginalFilename(); System.out.println("原始文件名:" + fileName); // 新文件名 String newFileName = UUID.randomUUID() + fileName; if (!files[i].isEmpty()) { try { FileOutputStream fos = new FileOutputStream(path + newFileName); InputStream in = files[i].getInputStream(); int b = 0; while ((b = in.read()) != -1) { fos.write(b); } fos.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("上传图片到:" + path + newFileName); list.add("img/"+newFileName); } return list; } /** * 上传一个文件或图片 * 上传多个文件或图片上传在项目路径下的img文件夹在 * !!!!!!重新部署项目实效,因为文件夹被删除 * @param file * @param request * @return */ public static String upfile(CommonsMultipartFile file,HttpServletRequest request){ // 获得项目的路径 ServletContext sc = request.getSession().getServletContext(); // 上传位置 String path = sc.getRealPath("/img") + File.separatorChar; // 设定文件保存的目录 File f = new File(path); if (!f.exists()) f.mkdirs(); // 获得原始文件名 String fileName = file.getOriginalFilename(); System.out.println("原始文件名:" + fileName); // 新文件名 String newFileName = UUID.randomUUID() + fileName; if (!file.isEmpty()) { try { FileOutputStream fos = new FileOutputStream(path + newFileName); InputStream in = file.getInputStream(); int b = 0; while ((b = in.read()) != -1) { fos.write(b); } fos.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("上传图片到:" + path + newFileName); return "img/"+newFileName; } /** * 下载 * @param request * @param response * @param filename * @return */ public static void downFile(HttpServletRequest request, HttpServletResponse response,String filename) { // 得到要下载的文件名 String fileName = filename.substring(4); try { fileName = new String(fileName.getBytes("iso8859-1"), "UTF-8"); // 获取上传文件的目录 ServletContext sc = request.getSession().getServletContext(); // 上传位置 String fileSaveRootPath = sc.getRealPath("/img"); System.out.println(fileSaveRootPath + "\\" + fileName); // 得到要下载的文件 File file = new File(fileSaveRootPath + "\\" + fileName); // 如果文件不存在 if (!file.exists()) { request.setAttribute("message", "您要下载的资源已被删除!!"); System.out.println("您要下载的资源已被删除!!"); return ; } // 处理文件名 String realname = fileName.substring(fileName.indexOf("_") + 1); // 设置响应头,控制浏览器下载该文件 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8")); // 读取要下载的文件,保存到文件输入流 FileInputStream in = new FileInputStream(fileSaveRootPath + "\\" + fileName); // 创建输出流 OutputStream out = response.getOutputStream(); // 创建缓冲区 byte buffer[] = new byte[1024]; int len = 0; // 循环将输入流中的内容读取到缓冲区当中 while ((len = in.read(buffer)) > 0) { // 输出缓冲区的内容到浏览器,实现文件下载 out.write(buffer, 0, len); } // 关闭文件输入流 in.close(); // 关闭输出流 out.close(); } catch (Exception e) { } } }

6.存入之后在jsp页面通过img标签显示<img alt="img" src="//数据库中存入的路径" width="100">

7.下载就是将图片的路径传入controller层中一个方法,调用工具类中的downFile方法,就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章