如何在spring mvc中上传图片并显示出来

如何在spring mvc中上传图片并显示出来
可以使用组件上传JspSmartUpload.这是一个类.

  
  
f1" id=" f1" >
用户名: 密码: 相片: 相片: 这里直接通过表单提交给servlet访问,spring中的话需要配置(一般就不用servlet了,自行配置). 以上在JSp页面中,以下是servlet/action中的代码,由于采用了 spring框架,怎么做你知道的(没有servlet但是有action). package com.demo.servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.File; import com.jspsmart.upload.Files; import com.jspsmart.upload.Request; import com.jspsmart.upload.SmartUpload; public class UploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SmartUpload su = new SmartUpload(); //初始化 su.initialize(this.getServletConfig(), request, response); try { //限制格式 //只允许哪几种格式 su.setAllowedFilesList("jpg,JPG,png,PNG,bmp,gif,GIF"); //不允许哪几种格式上传,不允许exe,bat及无扩展名的文件类型 //su.setDeniedFilesList("exe,bat,,"); //限制大小,每个上传的文件大小都不能大于100K su.setMaxFileSize(100*1024); //上传文件的总大小不能超过100K //su.setTotalMaxFileSize(100*1024); //上传 su.upload(); //唯一文件名 //得到文件集合 Files files = su.getFiles(); for(int i=0;i { //获得每一个上传文件 File file = files.getFile(i); //判断客户是否选择了文件 if(file.isMissing()) { continue; } //唯一名字 Random rand = new Random(); //String fileName = System.currentTimeMillis()+""+rand.nextInt(100000)+"."+file.getFileExt(); String fileName = UUID.randomUUID()+"."+file.getFileExt(); //以当前日期作为文件夹 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dirPath = sdf.format(new Date()); //获得物理路径 String realDirPath= this.getServletContext().getRealPath(dirPath); java.io.File dirFile = new java.io.File(realDirPath); //判断是否存在 if(!dirFile.exists()) { //创建文件夹 dirFile.mkdir(); } //保存 file.saveAs("/"+dirPath+"/"+fileName); //file.saveAs("/uploadFiles/"+fileName); } //原名保存 //su.save("/uploadFiles"); } catch (Exception e) { System.out.println("格式错误"); } //获得用户名 Request req = su.getRequest(); String username = req.getParameter("username"); System.out.println(username); } /** * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } } 特别注意导的包是JspSmartUpload中的还是java.io.*中的. 再次说明,这段代码是servlet中的,spring中的action可以剪切以上的一部分.请自行调整.









要在Spring MVC实现图片上传功能并在页面上显示图片,可以按照以下步骤进行: 1. 配置文件上传组件。可以使用Commons FileUpload或Servlet 3.0+文件上传API。 2. 创建一个控制器方法来处理文件上传请求。该方法应该使用@RequestParam注释来获取上传的文件。 3. 在控制器方法,将上传的文件保存到服务器文件系统上的某个位置。 4. 在控制器方法,将文件的URL或路径存储到数据库。 5. 创建一个JSP页面显示存储在服务器上的图片。可以使用<img>标签来显示图片,其src属性应该是服务器上图片的URL或路径。 下面是一个示例控制器方法,用于处理文件上传请求并将文件路径存储到数据库: ```java @RequestMapping(value = "/uploadImage", method = RequestMethod.POST) public ModelAndView uploadImage(@RequestParam("file") MultipartFile file) { //保存文件到服务器文件系统上的某个位置 String filePath = "/path/to/save/file/" + file.getOriginalFilename(); try { file.transferTo(new File(filePath)); } catch (IOException e) { e.printStackTrace(); } //将文件路径存储到数据库 //... //返回视图 ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("imageDisplay"); modelAndView.addObject("imageUrl", filePath); return modelAndView; } ``` 然后,可以在JSP页面使用以下代码来显示图片: ```html <img src="${imageUrl}" alt="Uploaded Image"> ``` 其,${imageUrl}是控制器方法返回的文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值