I am trying use struts.upload api to create a small app to upload a file and write it to a specific folder. It seems like my app is doing everything but writing to the physical drive.
Can anyone see why the file is not being written to the /repository folder under webroot?
fyi, i'm using jdk 1.4 and tomcat 4.1
here is the UploadFile.java that should write the file to the directory specified but does not. I've highlighted the part that seems to not work:
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.i
utputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class UploadFile extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
UploadForm theForm = (UploadForm) form;
//retrieve the file representation
FormFile file = theForm.getTheFile();
//retrieve the file name
String fileName = file.getFileName();
//retrieve the content type
String contentType = file.getContentType();
System.out.println(contentType); // temporary debugging
//retrieve the file size
String size = (file.getFileSize() + " bytes");
String data = null;
try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
//write the file to the file specified
OutputStream bos = new FileOutputStream("/repository");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data =
"The file has been written to \""
+ "/repository"
+ "\"";
}
//close the stream
stream.close();
} catch (FileNotFoundException fnfe) {
return null;
} catch (IOException ioe) {
return null;
}
return mapping.findForward("display");
}
}
Can anyone see why the file is not being written to the /repository folder under webroot?
fyi, i'm using jdk 1.4 and tomcat 4.1
here is the UploadFile.java that should write the file to the directory specified but does not. I've highlighted the part that seems to not work:
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.i
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class UploadFile extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
UploadForm theForm = (UploadForm) form;
//retrieve the file representation
FormFile file = theForm.getTheFile();
//retrieve the file name
String fileName = file.getFileName();
//retrieve the content type
String contentType = file.getContentType();
System.out.println(contentType); // temporary debugging
//retrieve the file size
String size = (file.getFileSize() + " bytes");
String data = null;
try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
//write the file to the file specified
OutputStream bos = new FileOutputStream("/repository");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data =
"The file has been written to \""
+ "/repository"
+ "\"";
}
//close the stream
stream.close();
} catch (FileNotFoundException fnfe) {
return null;
} catch (IOException ioe) {
return null;
}
return mapping.findForward("display");
}
}