关于uploadify图片上传把图片名称保存到数据库

无锡酱排骨 发布于 2013/05/22 21:33
阅读 1K+
收藏 0

我用了uploadify上传多张图片成功后如何获取图片名称,然后把多张图片按";"进行分割,保存到数据库的一个字段中的啊,我这个是实现先上传图片然后提交发表问题的时候一起把图片名称保存到数据库,我刚学JAVA不久,还请各位不吝赐教,谢谢了

JAVA代码:

@RequestMapping(value ="/upload")  
  @ResponseBody
  public String upload(HttpServletRequest request, HttpServletResponse response){
    Map<String, Object> map = new HashMap<String, Object>();
    String result="";  
    String path = request.getSession().getServletContext().getRealPath("upload");
    System.out.println("path:"+path);
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();    
    File file1 = new File(path);    
    if (!file1.exists()) {    
        file1.mkdirs();    
    }    
    String fileName = null;
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {   
      MultipartFile mf = entity.getValue();
      fileName = mf.getOriginalFilename();
      System.out.println("fileName:"+fileName);
      String extName = "";
      if (fileName.lastIndexOf(".") >= 0) {
        extName = fileName.substring(fileName.lastIndexOf("."));
        System.out.println("extName:"+extName);
    }
      String newFileName = new Date().getTime()+extName;
      path = request.getSession().getServletContext().getRealPath("upload")+"/";
      System.out.println("path1:"+path);
      File uploadFile = new File(path + newFileName);
      System.out.println("path+newFileName:"+path+newFileName);
      try {  
        FileCopyUtils.copy(mf.getBytes(), uploadFile);
        result = "上传成功";
      } catch (IOException e) {
        logger.error(e);
        e.printStackTrace();
        result = "上传失败";
    }
    }
    return result;
  } 

jsp页面:

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Upload</title>

<!--装载文件-->
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="scripts/swfobject.js"></script>
<script type="text/javascript" src="scripts/jquery.uploadify.v2.1.4.min.js"></script>

<!--ready事件-->
<script type="text/javascript">
    $(document).ready(function() {
        $("#uploadify").uploadify({
            'uploader' : 'scripts/uploadify.swf',
            'script' : 'upload.do',//后台处理的请求
            'cancelImg' : 'images/cancel.png',
            'folder' : 'uploads',//您想将文件保存到的路径
            'queueID' : 'fileQueue',//与下面的id对应
            'queueSizeLimit' : 3,//上传队列中所允许的文件数量。
            'fileDesc' : 'jpg文件或gif文件',
            'fileExt' : '*.jpg;*.gif', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc
            'auto' : true,
            'sizeLimit': 200 * 1024 * 1024,
            'multi' : true,
            'simUploadLimit' : 3,//允许同时上传的文件数量
            'buttonText' : 'addPicture',
           'onComplete': function(event, ID, fileObj, response, data) {
                //在传输过程完成之后  提示显示成功上传
                 alert(fileObj.name + "上传"+response);
            }
        });
    });
</script>
</head>

<body>
    <div id="fileQueue"></div>
    <input type="file" name="uploadify" id="uploadify" />
    <p>
        <a href="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a>&nbsp;
        <a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消所有上传</a>
    </p>
</body>
</html>


加载中
返回顶部
顶部