Issue
please i need your help in this AS3 and Java Code. The program is meant to upload a picture from a flex app to a remote server. The code works well, but i need to add something, i am looking for a way to send and extra parameter along with the image. Lets say i want to send the username of the person uploading the image along with the image. Please how do i send it from the client side using AS3 and how do i receive the data on the servlet.
Here is AS3 code
private var fileRef:FileReference = new FileReference();
private var servletTarget:URLRequest = new URLRequest(urlpath+"Comeboard/UploadImage");
private function fileBrowse():void
{
fileRef.addEventListener(Event.SELECT, onSelect);
fileRef.addEventListener(ProgressEvent.PROGRESS,progressHandler);
//just a label
uploadStatus.text = "Browsing the File System";
//a text field
fileLocation.text = "";
fileRef.browse();
}
private function onSelect(event:Event):void
{
uploadStatus.text = "File selected";
fileLocation.text = fileRef.name;
}
private function progressHandler(event:ProgressEvent):void
{
uploadStatus.text = "The file is " + percentLoaded + "% loaded";
}
private function fileUpload():void
{
//assuming this is the data i want to send
var data:String = "webdezzi";
uploadStatus.text = "Uploading....";
var menuURLVars:URLVariables = new URLVariables();
servletTarget.method = URLRequestMethod.POST;
fileRef.upload(servletTarget);
}
Here is the Code on the Servlet. Please pay attention to the multi line comment. Thanks in advance.
// Import required java libraries
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
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;
public class UploadImage extends HttpServlet {
String boardname = null;
private boolean isMultipart;
private String filePath;
private int maxFileSize = 1000 * 1024;
private int maxMemSize = 4 * 1024;
private File file ;
private static final long serialVersionUID = 1L;
public void init( )
{
// Get the file location where it would be stored.
filePath =
getServletContext().getRealPath("/");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException
{
// Check that we have a file upload request
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
if( !isMultipart )
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
System.out.println("kkkkkkkk"+fi.getFieldName());
if (fi.isFormField())
{
/*****************************************************
* i beleive this is where i am supposed to
* process the data i receive.
* i might be wrong though, *
******************************************************/
processFormField(fi);
}
if ( !fi.isFormField () )
{
processUploadedFile(fi);
}
}
out.println("</body>");
out.println("</html>");
}
catch(Exception ex)
{
out.println(ex.getMessage());
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException
{
throw new ServletException("GET method used with " +
getClass( ).getName( )+": POST method required.");
}
private void processFormField(FileItem item)
{
String name = item.getFieldName();
String value = item.getString();
System.out.println("Item name: " + name + " ; value: " + value);
}
private void processUploadedFile(FileItem item) throws Exception
{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
boolean writeToFile = true;
if (sizeInBytes > (5 * 1024 * 1024))
{
writeToFile = false;
}
// Process a file upload
if (writeToFile)
{
//File uploadedFile = new File(filePath + fileName);
File uploadedFile = new File(filePath + boardname);
if (!uploadedFile.exists())
{
uploadedFile.createNewFile();
}
item.write(uploadedFile);
}
else
{
System.out.println("Trying to write a large file.");
}
}
}
Solution
Sotirios Delimanolis advice helped solve the problem, but i would still try other suggestions on this.
In the AS3
var header:URLRequestHeader = new URLRequestHeader("username", username);
servletTarget.requestHeaders.push(header);
In the Servlet
String username = request.getHeader("username");
Answered By - Solomon Ubani D King
Answer Checked By - Marilyn (JavaFixing Volunteer)