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
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.