Ajax asynchronous upload and jqueryajax asynchronous upload in jquery
After searching for a long time, I finally found the ajax asynchronous file upload method on the Internet. However, most php files on the Internet are similar to those written in struts2, hoping to help java learners. I uploaded music files.
The js file ajaxfileupload. js is the main file and must be imported.
Jsp page, where I also hide the div *****************************
<% @ Page language = "java" import = "java. util. * "pageEncoding =" 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">
Action upload background code ************************************ *
Package action; import java. io. file; import java. io. IOException; import java. io. outputStream; import org. apache. commons. io. fileUtils; import org. apache. struts2.json. annotations. JSON; import org.farng.pdf. MP3File; import org.farng.pdf. tagException; import org.farng.pdf. id3.20.actid3v2; import org.farng.pdf. id3.ID3v1; import org.farng.pdf. lyrics3.w.actlyrics3; import com. base. baseAction; import com. opensymp Hony. xwork2.ActionSupport; public class Upload extends BaseAction {private static final long serialVersionUID =-4848248679889814408L; private String fileName; private File upload; public File getUpload () {return upload ;} public void setUpload (File upload) {this. upload = upload;} public void setUploadFileName (String fileName) {this. fileName = fileName;}/** song upload/upload operation */public void uploadAction () thro Ws IOException {System. out. println ("this method is enabled! "); String targetDirectory =" D: \ upload "; System. out. println (upload); File target = new File (targetDirectory, fileName); FileUtils. copyFile (upload, target); String path = targetDirectory + "\" + fileName; try {MP3File file = new MP3File (path); // 1, lyrics implements actid3v2 id3v2 = file. getID3v2Tag (); ID3v1 id3v1 = file. getID3v1Tag (); String ss = ""; if (id3v2! = Null) {System. out. println ("id3v2"); ss = id3v2. getAlbumTitle () + ";" + id3v2. getSongTitle () + ";" + id3v2. getLeadArtist (); // String str = "{'msg ','" + ss + "'}"; String str = ss; outPut (str); System. out. println (id3v2. getAlbumTitle (); // album name System. out. println (id3v2. getSongTitle (); // song name System. out. println (id3v2. getLeadArtist (); // artist} else {System. out. println ("id3v1"); System. out. println (id3v1. getAlbumTitle (); System. out. println (id3v1. getSongTitle (); System. out. println (id3v1. getLeadArtist ();} AbstractLyrics3 lrc3Tag = file. getLyrics3Tag (); if (lrc3Tag! = Null) {String lyrics = lrc3Tag. getSongLyric (); System. out. println (lyrics) ;}} catch (IOException e) {e. printStackTrace ();} catch (TagException e) {e. printStackTrace ();} System. out. println ("over ");}}
Struts. xml configuration file *********************************
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name = "struts. i18n. encoding "value =" UTF-8 "/> // sets the maximum number of uploaded files <constant name =" struts. multipart. maxSize "value =" 10485760 "/> <package name =" upload "namespace ="/"extends =" struts-default "> <action name =" uploadAction "class =" action. upload "method =" uploadAction "> <result name =" success ">/index. jsp </result> </action> </package> </struts>
Later, after debugging, I found that Firefox and ie are incompatible, resulting in no execution results. Therefore, I uploaded ajaxfileupload. js and modified the source code.
The above section describes ajax asynchronous upload in jquery, hoping to help you.