Pages

Tuesday, June 23, 2009

Multiple Uploads

Note: what ever the code is given totally worked
replace all -&lt- with < -&gt- with >

To upload multiple images, videos we can use the following code..........

-------------------------------------------------------------------------------------------
HTML Code:
-------------------------------------------------------------------------------------------
-&lt- input type="file" name="Flash_Path" onkeypress="return false" / -&gt-
-&lt- input type="file" name="Image_Path" onkeypress="return false" / -&gt-
-&lt- input type="text" name="name" / -&gt-

-------------------------------------------------------------------------------------------
JAVA Code:
-------------------------------------------------------------------------------------------

use IMPORTS:

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;


public static Map UploadCategoryImage(HttpServletRequest request) {

// System.out.println(" UploadFile() ");
String flashPath = "";
// Map object is used store form fields names[ key ] and their values [ values ].
Map hMap = new HashMap();
// diskFile object is used to store uploaded file into server.
DiskFileUpload diskFile = new DiskFileUpload();

// items list object is used to store form fields which are of type File.
List items = new ArrayList();

// Part One: to parse input file and other input form fields
try {
items = diskFile.parseRequest(request);
} catch (Exception e) {
e.printStackTrace();
}
if(items.size() > 0 ) {
Iterator itr = items.iterator();
StringBuilder directory=new StringBuilder();
directory.append("./../webapps/HMTV/videos/archieves/category");
File directoryFile=new File(directory.toString());
directoryFile.mkdirs();


/**
* Purpose is to store all parameters from uploadPortlet_view.jsp into HashMap object.
*/
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
System.out.println(" form field name : " + item.getFieldName());
System.out.println(" form field value : " + item.getString());

hMap.put(item.getFieldName(), item.getString());

} else {
try {
String itemName = item.getName();

System.out.println("categoryName 1 >--------> " + itemName);
if (itemName.indexOf("\\") != -1) {
itemName = itemName.substring(itemName.lastIndexOf("\\") + 1);
} else if (itemName.indexOf("/") != -1) {
itemName = itemName.substring(itemName.lastIndexOf("/") + 1);
}

// System.out.println("itemName 2 >--------> " + itemName);

flashPath = directory.append("/").toString()+itemName;
System.out.println("flashPath 2 >--------> " + flashPath);
// File nFile = new File(flashPath);
// nFile.mkdirs(); // create directory if does not exists initially.
hMap.put(item.getFieldName(), flashPath);
File savedFile = new File("../webapps"+flashPath);
item.write(savedFile);
} catch (Exception e) {
System.out.println(" errrrrrrr ");
e.printStackTrace();
hMap=null;
}

} // else
} // while
}
// System.out.println("hMap ::::::::::::::: " + hMap);

return hMap;
}

------------------------------------------------------------------
Use this method to retrieve all files and store files in specified path.
Change necessary paths as per your requirement.


The above method gives hash map for above HTML we can retrieve the input fields in the following way

in ActionServlet or action handling class use

Map hMap = -&lt- classname -&gt-.UploadCategoryImage(httpRequest);

imagePath = (String) hMap.get("Image_Path"); ---- form field(file)
flashPath = (String) hMap.get("Flash_Path"); ---- form field(file)
name= (String) hmap.get("name"); -- form field(general)

now we can store these values to store in database or for some logical usage.

Run videos in web page

To run flv videos in your web site -- use the following code

NOTE: -&lt- represents < -&gt- represents >



-&lt-object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="800" height="420"
name="aaa" id="aaa"-&gt-
-&lt-param name="movie" value="/VideoArchieves/js/flvplayer.swf" /-&gt-
-&lt-param name="quality" value="high" /-&gt-
-&lt-param name="allowfullscreen" value="true" /-&gt-
-&lt-param name="uimode" value="true"/-&gt-
-&lt-param name="loop" value="9999"/-&gt-
-&lt-param name="flashvars" value="file=/HMTV/videos/archieves/<%=content%>&image=/HMTV/videos/archieves/preview.jpg&shuffle=false&showdownload=false&frontcolor=0xFFFFFF&repeat=true&autostart=false&showicons=true&showvolume=true&thumbsinplaylist=true&linktarget=_blank&backcolor=0x3E2B00&lightcolor=0xFFFFFF&bufferlength=10" /-&gt-
-&lt-param name="wmode" value="opaque" /-&gt-
-&lt-embed id="aaa" src="/VideoArchieves/js/flvplayer.swf" loop="9999" allowfullscreen="true" uimode="true" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="800" height="420" wmode="opaque" flashvars="file=/HMTV/videos/archieves/<%=content%>&image=/HMTV/videos/archieves/preview.jpg&shuffle=false&showdownload=false&frontcolor=0xFFFFFF&repeat=true&autostart=false&showicons=true&showvolume=true&thumbsinplaylist=true&linktarget=_blank&backcolor=0x3E2B00&lightcolor=0xFFFFFF&bufferlength=10"-&gt--&lt-/embed-&gt-
-&lt-/object-&gt-