permaculture playing cards
The moose likes Java in General and the fly likes request cannot be resolved. any imports missing?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "request cannot be resolved. any imports missing??" Watch "request cannot be resolved. any imports missing??" New topic

request cannot be resolved. any imports missing??

Rohit Kumar
Ranch Hand

Joined: Jul 19, 2007
Posts: 35
Hi ,

I have this java class to upload files to a location on my local machine. when I used the same code with JSP it is wporking fine. If I use the same code with java class it is showing exceptions fpr request object like request cannot be resolved.

Do i need to import anything.Do please suggest me

package oosurance.trans;


public class FileUpload{

String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
byteRead =, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
//String folder = "C:/upload/images/";
File folder = new File("C:/upload/images");
if (! folder.exists()) folder.mkdirs();

FileOutputStream fileOut = new FileOutputStream( folder.getCanonicalPath() + File.separatorChar + saveFile);

//out.print("Saved here: " + saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336


These are both methods of ServletRequest. You can't use them outside of a servlet container. If there is no request, you can't call any methods on it!

Why are you trying to "upload" file using a normal Java application? You could just use to interact with the file system. What is it your are trying to do?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: request cannot be resolved. any imports missing??
It's not a secret anymore!