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

可以使用组件上传JspSmartUpload.这是一个类.
<form name="f1" id="f1" action="/demo/servlet/UploadServlet" method="post" enctype="multipart/form-data">
      <table border="0">
        <tr>
          <td>用户名:</td>
          <td><input type="text" name="username" id="username"></td>
        </tr>
        <tr>
          <td>密码:</td>
          <td><input type="password" name="password" id="password"></td>
        </tr>
         <tr>
          <td>相片:</td>
          <td><input type="file" name="pic" id="pic"></td>
        </tr>
        <tr>
          <td>相片:</td>
          <td><input type="file" name="pic2" id="pic2"></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><input type="submit"></td>
        </tr>
      </table>
    </form>
这里直接通过表单提交给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<files.getCount();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. <br>
	 *
	 * 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可以剪切以上的一部分.请自行调整.
希望能够帮助你.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值