Getting File in Server Program but not uploding it...Need help 
May 22nd, 2007, 12:13 PM
| | Newbie | | Join Date: May 2007
Posts: 4
| |
HI friends,
I am using the Flex to upload files to server. I m getting all the details about the file, but I m not able to upload it to Server. Here is the code i m using for both flex & for Struts: -
import java.io.File;
-
import java.io.PrintWriter;
-
import java.util.ArrayList;
-
import java.util.Enumeration;
-
import java.util.Iterator;
-
import java.util.List;
-
-
import javax.servlet.ServletContext;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import org.apache.commons.fileupload.FileItem;
-
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-
import org.apache.commons.fileupload.servlet.ServletFileUpload;
-
import org.apache.struts.action.Action;
-
import org.apache.struts.action.ActionForm;
-
import org.apache.struts.action.ActionForward;
-
import org.apache.struts.action.ActionMapping;
-
-
public class ServerFileUpload extends Action {
-
@Override
-
public ActionForward execute(ActionMapping mapping, ActionForm form,
-
HttpServletRequest request, HttpServletResponse response) throws Exception {
-
try {
-
DiskFileItemFactory factory = new DiskFileItemFactory();
-
ServletFileUpload upload = new ServletFileUpload(factory);
-
/*
-
* both file size & attachments size should not be greater than 10MB.
-
*/
-
upload.setFileSizeMax(10*1024*1024);
-
upload.setSizeMax(10*1024*1024);
-
List items = upload.parseRequest(request);
-
Enumeration e = request.getParameterNames();
-
while(e.hasMoreElements()){
-
System.out.println("Parameter Name are "+e.nextElement());
-
}
-
-
Iterator iter = items.iterator();
-
List lstFiles = new ArrayList();
-
while (iter.hasNext()) {
-
FileItem item = (FileItem) iter.next();
-
System.out.println("in while");
-
-
if (!item.isFormField()) {
-
String fieldName = item.getFieldName();
-
System.out.println("Field Name is :" + fieldName);
-
String filePath = item.getName();
-
System.out.println(filePath);
-
long size = item.getSize();
-
System.out.println("The size of the File is : "+size);
-
-
if(filePath.equals(""))
-
continue;
-
-
String contentType = item.getContentType();
-
System.out.println("The File Content Type Are "+contentType);
-
-
ServletContext ctx = getServlet().getServletContext();
-
-
String folder = ctx.getRealPath("WEB-INF/uploadedfiles/" +fieldName);
-
-
System.out.println("real path "+folder);
-
File f=new File(folder);
-
if(!f.exists()){
-
System.out.println("Folder Does Not Exit");
-
f.mkdir();
-
}
-
File file = new File(filePath);
-
boolean status= file.renameTo(new File(folder, file.getName()));
-
System.out.println("Copy Status is "+status);
-
-
// System.out.println(folder + "/" + file);
-
-
File uploadedFile = new File(filePath);
-
item.write(uploadedFile);
-
lstFiles.add(filePath);
-
}
-
}
-
PrintWriter out = response.getWriter();
-
response.setContentType("text/xml");
-
out.print("<result>success</result>");
-
} catch (Exception e) {
-
System.out.println("Exception in FileUpload"+e.getMessage());
-
}
-
return null;
-
}
-
}
-
here is the flex code: -
<?xml version="1.0" encoding="utf-8"?>
-
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="initApp()">
-
<mx:Script>
-
<![CDATA[
-
import mx.controls.Alert;
-
import mx.utils.ObjectUtil;
-
import flash.events.*;
-
import flash.net.FileReference;
-
import flash.net.URLRequest;
-
-
-
private var fileRef:FileReference;
-
-
private function initApp():void {
-
fileRef = new FileReference();
-
fileRef.addEventListener(Event.CANCEL, traceEvent);
-
fileRef.addEventListener(Event.COMPLETE, completeEvent);
-
fileRef.addEventListener(Event.SELECT, selectEvent);
-
fileRef.addEventListener(IOErrorEvent.IO_ERROR, traceEvent);
-
fileRef.addEventListener(Event.OPEN, traceEvent);
-
fileRef.addEventListener(ProgressEvent.PROGRESS, progressEvent);
-
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, traceEvent);
-
}
-
-
private function traceEvent(event:Event):void {
-
var tmp:String = "================================\n";
-
ta.text += tmp + event.type + " event:" + mx.utils.ObjectUtil.toString(event) + "\n" ;
-
ta.verticalScrollPosition += 20;
-
}
-
-
private function ioErrorEvent(event:IOErrorEvent):void{
-
Alert.show("IOError:" + event.text);
-
traceEvent(event);
-
}
-
-
private function selectEvent(event:Event):void{
-
btn_upload.enabled = true;
-
traceEvent(event);
-
filename.text = fileRef.name;
-
-
progressBar.setProgress(0, 100);
-
progressBar.label = "Loading 0%";
-
}
-
-
private function progressEvent(event:ProgressEvent):void {
-
progressBar.setProgress(event.bytesLoaded, event.bytesTotal);
-
traceEvent(event);
-
}
-
-
private function completeEvent(event:Event):void {
-
progressBar.label = "Complete.";
-
filename.text += " uploaded";
-
traceEvent(event);
-
btn_upload.enabled = false;
-
btn_cancel.enabled = false;
-
}
-
-
private function uploadFile(endpoint:String):void {
-
var param:String = "Ashish";
-
var req:URLRequest = new URLRequest(endpoint);
-
req.method = URLRequestMethod.POST;
-
fileRef.upload(req, param, false);
-
progressBar.label = "Uploading...";
-
btn_cancel.enabled = true;
-
}
-
-
]]>
-
</mx:Script>
-
-
<mx:Panel title="Flex 2 File Uploading Demo" width="100%" height="100%" >
-
<mx:Form>
-
-
<mx:FormItem label="Upload URL:" id="uploadurl">
-
<mx:TextInput id="uploadURL" width="100%" text="http://localhost:8080/visionmail/upload.do" enabled="true" />
-
</mx:FormItem>
-
-
<mx:FormItem label="Selected File:" id="frmfilename">
-
<mx:Label id="filename"/>
-
</mx:FormItem>
-
-
<mx:FormItem label="Upload By:" id="author">
-
<mx:TextInput id="ti_author" text="Author" />
-
</mx:FormItem>
-
-
<mx:FormItem direction="horizontal" width="100%">
-
<mx:Button width="80" label="Browse" click="fileRef.browse()" />
-
<mx:Button width="80" label="Upload" id="btn_upload" enabled="false" click="uploadFile(uploadURL.text)" />
-
<mx:Button width="80" label="Cancel" id="btn_cancel" enabled="false" click="fileRef.cancel()" />
-
</mx:FormItem>
-
-
<mx:HRule width="100%" tabEnabled="false"/>
-
-
<mx:FormItem label="Progress:">
-
<mx:ProgressBar id="progressBar" mode="manual" />
-
</mx:FormItem>
-
-
<mx:FormItem label="Events:">
-
<mx:TextArea id="ta" width="350" height="200" />
-
</mx:FormItem>
-
-
</mx:Form>
-
-
</mx:Panel>
-
</mx:Application>
-
-
can somebody tell me what is the problem here.
| 
May 22nd, 2007, 11:32 PM
|  | Moderator | | Join Date: Jul 2006 Location: California!!!
Posts: 898
| | | re: Getting File in Server Program but not uploding it...Need help
Thats not the way it works around here. I'm sorry but its not our job to find out whats wrong with your code. Normally, people resent us with their error and a partial bit of code (not the full thing) and we help them our and/or point them in the right direction so that they may solve the problem themselves. Are there any specific problems you have?
| 
August 21st, 2008, 09:59 AM
| | Newbie | | Join Date: Aug 2008
Posts: 1
| | | re: Getting File in Server Program but not uploding it...Need help Quote: |
Originally Posted by kestrel Thats not the way it works around here. I'm sorry but its not our job to find out whats wrong with your code. Normally, people resent us with their error and a partial bit of code (not the full thing) and we help them our and/or point them in the right direction so that they may solve the problem themselves. Are there any specific problems you have? |
Problem is
List items = upload.parseRequest(request);
returning empty list.
When same code is used in JSP it work fine. Problem is when URL is struts action. Then in the action class i am not able to get the contents.
| 
November 24th, 2008, 05:21 AM
| | Newbie | | Join Date: Nov 2008 Location: Chennai,India
Posts: 3
| | | re: Getting File in Server Program but not uploding it...Need help
I want to upload a file using action script & servlet to a server?
I am new to action script & i don't know where to start.
please guide me...
Thanks
| 
July 2nd, 2009, 05:59 AM
| | Newbie | | Join Date: Sep 2008
Posts: 6
| | | re: Getting File in Server Program but not uploding it...Need help
Dear vijendrahs ,
I am also facing same problem exactly... kindly if u found the solution for it.. please share with me...
Thanks and Regards
Diny Jayas
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|