- This topic has 5 replies, 2 voices, and was last updated 16 years, 1 month ago by Gabriel Khoa Bui.
-
AuthorPosts
-
Gabriel Khoa BuiMemberI wanna get the TOMCAT_HOME working dir. Can anyone help me some code?
Thanks!
Riyad KallaMemberYou need to be more specific, I don’t understand what you are asking for.
Gabriel Khoa BuiMemberI wanna upload some images to my project directory. But result I got was the images which stored in “bin” dir of TOMCAT server or in C hardisk.
I read some tutorial that showed me how to config the “temdir” of my custom processor in stuts-config.xml file. According to that tutorial, all upload files which will be stored in specific temdir. But I got nothing. I just find a way to store them in my project dir by finding the absolute path of TOMCAT server. Example: String path = “C:\Program Files\Apache Tomcat Server”(TOMCAT_HOME). Next, store images in “path+webbapps+project_name+upload”.
Gabriel Khoa BuiMemberAny help, plz!
Gabriel Khoa BuiMemberIf specify: “String path= null;” then the data will be written in %<TOMCAT_HOME>%\bin directory.
Here this is my code:package com.deth.struts.action;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;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;
import com.deth.struts.form.UploadForm;import deth.utilities.Constants;
/**
* @author deth
*/
public class UploadAction extends Action
{
/**
* @param mapping
* @param form
* @param request
* @param response
* @return success
* @throws Exception
*/
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
if (form instanceof UploadForm) {//this line is here for when the input page is upload-utf8.jsp,
//it sets the correct character encoding for the response
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase(“utf-8”)))
{
response.setContentType(“text/html; charset=utf-8″);
}UploadForm theForm = (UploadForm) form;
//retrieve the text data
String text = theForm.getTheText();
//retrieve the query string value
String queryValue = theForm.getQueryParam();
//retrieve the file representation
FormFile file = theForm.getTheFile();
//retrieve the file name
String fileName= file.getFileName();
//retrieve the content type
String contentType = file.getContentType();
//retrieve the file size
String size = (file.getFileSize() + ” bytes”);
String data = null;InputStream in = null;
OutputStream out = null;
System.out.println(“Begining…”);//Specify the needed path to write data in.
/*String filepath = “C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\px\\upload\\images\\”; */
String filepath = null;System.out.println(“File path” + filepath.toString());
String filename = filepath + fileName;
try
{
File fileImage = new File(filename);
// Check and see if duplicated.
for(int f = 0; fileImage.exists(); f++){
filename = fileName + fileName + f;
fileImage = new File(filename);
}
System.out.println(“Uploading….”);
// Get an input stream on the form file
in = file.getInputStream();
// Create an output stream to a file
out = new BufferedOutputStream (new FileOutputStream(fileImage));
byte[] buffer = new byte[512];
while (in.read(buffer) != -1)
{
out.write(buffer);
}
System.out.println(“upload success”);
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}
finally {
if (out != null) out.close( );
if (in != null) in.close( );
}
}
return mapping.findForward(Constants.SUCCESS);
}
}Please any help. Thanks!
Gabriel Khoa BuiMemberClosed.
-
AuthorPosts