wood burning stoves 2.0*
The moose likes Struts and the fly likes Problem with file separator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with file separator" Watch "Problem with file separator" New topic
Author

Problem with file separator

naveen chedella
Greenhorn

Joined: Dec 05, 2008
Posts: 8
<html:file> tag works fine when i deployed my application on windows server and client is upload the file from windows machine.It is giving "Unknown Source" once i deployed application on Linux server and client is upload the file from windows machine.

This is something to do with the File Separator.

Even though i changed the File Separator manually from "\" to "/" still its not working and it is giving "File Not Found Exception".

Please Help Me...

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

ItDoesntWorkIsUseless, TellTheDetails.

No way to help yet.
naveen chedella
Greenhorn

Joined: Dec 05, 2008
Posts: 8
This is my form bean:

public class LoadPowerForm extends ActionForm {
private String strForecastDataFile;

public void setStrForecastDataFile(String strForecastDataFile) {
this.strForecastDataFile = strForecastDataFile;
}
public String getStrForecastDataFile() {
return strForecastDataFile;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
String[] strFileDir = getFileName(getStrForecastDataFile()); //Here i am getting Unknown Source Problem
}

private String[] getFileName(String strAbsoluteFileName) {
String strFileDir[] = new String[2];
try
{
File file = new File(strAbsoluteFileName);
strFileDir[0] = file.getParent();
strFileDir[1] = strAbsoluteFileName.substring(file.getParent().length()+1);
}
catch(Exception e) {
//error message
}
return strFileDir;
}
}

//Here is my Jsp

External Power Forecast Data File : <html:file property="strForecastDataFile" name="LoadPowerForm"/>


This is perfectly worked once i deployed in windows environment but giving the problem only when i deployed in the linux environment.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.

html:file tags should use FormFile fields.
naveen chedella
Greenhorn

Joined: Dec 05, 2008
Posts: 8
But the FormFile will not give complete User File Path.I tested with the following example.
JSP:
External Power Forecast Data File : <html:file property="theFile" name="StrutsUploadForm "/> // I have Uploaded D:\EXT_FORECAST_DATA_21012009121212 file from the Browser


Form Bean:
public class StrutsUploadForm extends ActionForm
{
private FormFile theFile;
/**
* @return Returns the theFile.
*/
public FormFile getTheFile() {
return theFile;
}
/**
* @param theFile The FormFile to set.
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
}

Action Class:

package com.ngt.wpfs.actions;

import com.ngt.wpfs.forms.StrutsUploadForm;

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 StrutsUploadAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
StrutsUploadForm myForm = (StrutsUploadForm)form;
// Process the FormFile
FormFile myFile = myForm.getTheFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();

System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName); // This gives EXT_FORECAST_DATA_21012009121212.CSV only but i need Directory name and FileName to Retrive the data from the file in corressponding directory





return mapping.findForward("success");
}
}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I was serious--please UseCodeTags.

What would you do with the path from the client machine anyway? Nothing--and some browsers don't even *send* the entire path, so it doesn't matter if you have it or not.
naveen chedella
Greenhorn

Joined: Dec 05, 2008
Posts: 8
i needed to read the complete file name to display to the user that he has selected this particular file name and need to read the contents of the file and if the file is not valid then rename the file as Rejected and place it in the directory where client picked the actual file.Thats why i take it as String Property in the form bean and it is perfectly worked when application is deployed in the Windows Machine and client is accessed through browser in Windows machine but this is giving problem when i deployed on the Linux server and client is accessed in Windows machine.

Please help me in this regard.... And this is nothing to do the formfile and this is some thing regarding file separator
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Java will work with either separator. If you're dead-set on changing it, go ahead and change it with a replaceAll.

You *CANNOT* rely on the full file path being submitted: that's just a fact of life that has absolutely nothing to do with Struts.
 
 
subject: Problem with file separator