栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

uploadify java实现多文件上传和预览

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

uploadify java实现多文件上传和预览

本文实例为大家分享了java文件上传和预览实现代码,供大家参考,具体内容如下

1、下载uploadify插件

2、index.html

 
 
 
<@head/> 
 
 
 
#uploader { 
 position: relative; 
} 
 
#uploader_queue { 
 position: absolute; 
 width: 600px; 
 left: 200px; 
 top: 0; 
} 
 
 
 
 
 <@header/> 
 



上传 取消上传



<@footer/>

3、java文件

package com.frame.core.ctrl; 
 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Date; 
import java.util.Map; 
import java.util.UUID; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.apache.log4j.Logger; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.multipart.MultipartFile; 
import org.springframework.web.multipart.MultipartHttpServletRequest; 
import org.springframework.web.servlet.ModelAndView; 
 
@Controller 
public class loginCtrl { 
 private static Logger log = Logger.getLogger(loginCtrl.class); 
 @RequestMapping(value = "/goindex") 
 public ModelAndView goindex() { 
  ModelAndView mav = new ModelAndView("index"); 
  mav.addObject("name", "笑傲江湖"); 
  mav.addObject("projectName", "Freemarker框架"); 
  return mav; 
 } 
 @RequestMapping(value = "/login") 
 public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { 
  request.getSession().setAttribute("username", "身份认证成功"); 
  request.getRequestDispatcher("/index.jsp").forward(request, response); 
 } 
 @RequestMapping("/uploadAttach") 
 public void processUploadDir(ModelMap modelMap, 
   MultipartHttpServletRequest request, PrintWriter writer) throws Exception { 
  Map fileMap = request.getFileMap(); 
  String path = request.getSession().getServletContext().getRealPath("/");; 
  System.out.println("path:"+path); 
  Date currentTime = new Date(); 
  long prefix = currentTime.getTime(); 
  StringBuffer attachIds = new StringBuffer(); 
  for (Map.Entry f : fileMap.entrySet()) { 
   MultipartFile file = f.getValue(); 
   if (!isLegalFile(file)) { 
    String msg = "is a illegal file"; 
    throw new RuntimeException(msg); 
   } 
   String originalFileName = prefix + "_" + file.getOriginalFilename(); 
   File fileDir = new File(path + "/upload" + File.separator); 
   if (!fileDir.exists()) { 
    fileDir.mkdirs(); 
   } 
 
   File files = new File(path + "/upload" + File.separator 
     + originalFileName); 
   FileOutputStream fileOutputStream = null; 
   try { 
    fileOutputStream = new FileOutputStream(files); 
    fileOutputStream.write(file.getBytes()); 
    fileOutputStream.flush(); 
 
    attachIds.append(originalFileName + ","); 
 
   } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } catch (Exception e) { 
    e.printStackTrace(); 
   } finally { 
    if (fileOutputStream != null) { 
     try { 
      fileOutputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
   } 
 
  } 
 
  writer.write(attachIds.toString().substring(0,attachIds.toString().length()-1)); 
 } 
 private final String[] fileType = new String[]{".dat",".264",".h264",".mp4",".dav",".MP4",".AVI",".ts",".avi",".mpg",".rmvb",".flv",".rm",".mov",".wmv", 
   ".JPG",".bmp",".png",".BMP",".jpg",".PNG",".gif", 
   ".xlsx",".xls",".txt",".pdf",".doc",".docx", 
   ".rar",".zip",".7z"}; 
 private boolean isLegalFile(MultipartFile file) { 
  String originalFileName = file.getOriginalFilename(); 
  for(String ft : fileType) { 
   if (originalFileName.endsWith(ft)) { 
    return true; 
   } 
  } 
  return false; 
 } 
} 

效果图:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148990.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号