Uploadify Java implementation of multiple file upload and preview _java

Source: Internet
Author: User
Tags file upload rar stringbuffer

This example for you to share the Java file upload and preview implementation code, for your reference, the specific contents are as follows

1. Download Uploadify Plugin

2, index.html

<! DOCTYPE html>  

3, Java file

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", "Smile Proud"); 
  Mav.addobject ("ProjectName", "Freemarker framework"); 
 return MAV; @RequestMapping (value = "/login") public void Login (HttpServletRequest request,httpservletresponse response) throws 
  Servletexception, IOException {request.getsession (). setattribute ("username", "Successful identity authentication"); 
 Request.getrequestdispatcher ("/index.jsp"). Forward (request, response); @RequestMapping ("/uploadattach") public void Processuploaddir (Modelmap modelmap, multiparthttpservletrequest req 
  Uest, PrintWriter writer) throws Exception {map<string, multipartfile> 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<string, multipartfile> 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;  } 
}

Effect Chart:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.