smartupload实现多图上传

jsp页面

<form class="form-inline definewidth m20" action="<%=basePath %>upload" method="post" enctype="multipart/form-data">  
    图片一:<input type="file" name="myfile1">名称:<input type="text" name="title"><br> 
    图片二:<input type="file" name="myfile2">名称:<input type="text" name="title"><br> 
    图片三:<input type="file" name="myfile3">名称:<input type="text" name="title"><br> 
    图片四:<input type="file" name="myfile4">名称:<input type="text" name="title"><br> 
 <input type="submit" value="提交">   
</form>

<pre name="code" class="java">package com.test.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.marx.factory.DAOFactory;
import com.marx.vo.Image;

public class UploadServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK"); 
				//设置图片保存路径
				
				String filePath = getServletContext().getRealPath("/")+"Upload";
				System.out.println(filePath);
				File file = new File(filePath);
				if(!file.exists()){
					file.mkdir();
				}
				SmartUpload su = new SmartUpload();
				//初始化
				su.initialize(getServletConfig(), request, response);
				//限制文件上传大小
				su.setMaxFileSize(1024*1024*10);
				//设置允许上传文件格式
				su.setAllowedFilesList("jpg,gif");
				try {
					su.setDeniedFilesList("txt,doc");
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				String result = "上传成功";
				int count = 0;
				try {
					su.upload();//上传图片
					count = su.save(filePath);//得到上传文件个数
				} catch (SmartUploadException e) {
 					e.printStackTrace();
				}
				for(int i=0;i<count;i++){
                                        //这里将图片信息插入数据库中,比如图片地址,图片的名称
					com.jspsmart.upload.File tempFile = su.getFiles().getFile(i);
					String path = request.getContextPath();
					String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
					String fileurl = basePath+"Upload/"+tempFile.getFileName();//得到服务器端图片上传地址
					String[] title = su.getRequest().getParameterValues("title");
					Image img = new Image();
					img.setImgurl(fileurl);
					img.setTitle(title[i]);
					try {
						DAOFactory.getImgDaoinstance().insert(img);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				request.setAttribute("result", result);
				request.getRequestDispatcher("/admin/Img/add_success.jsp").forward(request, response);
		
	}

}



web.xml配置

 <servlet>
    <servlet-name>Upload</servlet-name>
    <servlet-class>com.test.servlet.UploadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Upload</servlet-name>
    <url-pattern>/upload</url-pattern>
  </servlet-mapping>
这样就利用smartupload插件实现了多图上传,并将信息插入数据库的功能了

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。