Hello,
I have problems with code I copied from Code in
org.apache.jetspeed.modules.actions.portlets.designer.HeaderAction.
I use Jetspeed 1.5 which installs turbine-2.2.jar
My adapted code looks like this:
private String saveFile(RunData rundata, HttpServletRequest request,
String icn,
String uploadLocation,
String uploadBOClass, String getDetailMethod,
String
uploadMethod, String uploadUpdateColumn) {
String errorMessage= null;
try {
// Code like the code in
org.apache.jetspeed.modules.actions.portlets.designer.HeaderAction
// The standard method to get multiform data can be used
in a servlet but does not work
// in Jetspeed portlets!!
String fileTypes[] =
{ "image/jpg",
"image/gif",
"image/jpeg",
"application/x-shockwave-flash",
"image/png",
"image/x-png"
};
FileItem fileItem =
rundata.getParameters().getFileItem("file");
String select=
rundata.getParameters().getString("select").trim();
String lookUpTableKey= "##" + ICNPortlet.CHOICE_SHOW +
icn + "##";;
String lookUpTable=
rundata.getParameters().getString(lookUpTableKey).trim();
O_I key= null;
if( lookUpTable != null ) {
key= translateChoiceIcn( select, lookUpTable);
// get selected value for the icn
}
else {
key= new XString(select);
}
if (fileItem != null) {
File file = new File(fileItem.getFileName());
String fileName= file.getName();
Log.info("Uploading " + fileName);
// to play it save:
int index = fileName.lastIndexOf("\\");
int index2 = fileName.lastIndexOf("//");
if (index > 0) {
fileName = fileName.substring(index +
1);
}
if (index2 > 0) {
fileName= fileName.substring(index2 +
1);
}
String webappPath=
request.getSession().getServletContext().getRealPath("/");
String path= null;
if( uploadLocation != null) {
path= webappPath + uploadLocation;
}
else {
path= webappPath;
}
File f = new File(path + fileName);
if (f.exists()) {
f.delete();
}
FileUploader fu = new FileUploader(); // a
jetspeed helper class
// the uploader creates files even when the user
types an invalid filename like dummy.jpg!
// ==> check length!
long len= file.length();
if( len == 0) {
errorMessage=
CU.getI18N("error.upload.error") + " " + fileName
+
CU.getI18N("error.upload.zero");
Log.error( errorMessage);
return errorMessage;
}
// the check for valid fileTypes does not work!!
// ==> check here
boolean hasUploaded= false;
index= fileName.lastIndexOf(".");
String fileExtension= fileName.substring(index +
1);
if( fileExtension.equals("jpg") ||
fileExtension.equals("gif") ||
fileExtension.equals("png") ||
fileExtension.equals("jpeg")) {
Log.info("Copy " + fileName + " to " +
path);
// hasUploaded = fu.upload(fileItem,
path, fileTypes);
FileInputStream is= new
FileInputStream(file);
FileOutputStream os= new
FileOutputStream(path + fileName);
copyFile( is, os, true);
hasUploaded= true;
}
else {
errorMessage=
CU.getI18N("error.upload.error") + " "
+ fileName +
CU.getI18N("error.upload.type");
Log.error( errorMessage);
return errorMessage;
}
if (hasUploaded == true) {
...
This works fine if I make an upload from another Windows Box to my
Windows Computer.
The log on my windows boxs (turbine.log) says:
2004-09-29 18:26:17,578 [http-8080-Processor25] INFO
JetspeedLoggingService - Uploading hofmann.jpeg
2004-09-29 18:26:17,593 [http-8080-Processor25] INFO
JetspeedLoggingService - Copy hofmann.jpeg to D:\Programme\Apache
Software Foundation\Tomcat 5.0\webapps\jetspeed\probusiness/empphotos/
You see, the first INFO line does not contain the path (you can see the
place where this entry is made in the code above).
I copied everything to a Linux-Server, but this time I failed!
The log on the linux server says (the german error message says that the
file length is 0):
2004-09-30 12:18:10,895 [TP-Processor10] INFO JetspeedLoggingService -
Uploading C:\tmp\aaaBanner1.jpg
2004-09-30 12:18:10,896 [TP-Processor10] ERROR JetspeedLoggingService -
Fehler beim Hochladen von aaaBanner1.jpg. Die Länge ist 0.
Here we have the full path!
Then I copied another image file to /tmp on the Linux server and entered
in the HTML Form
/tmp/hofmann.jpeg
2004-09-23 13:36:27,986 [TP-Processor12] INFO JetspeedLoggingService -
Uploading hofmann.jpeg
2004-09-23 13:36:27,986 [TP-Processor12] INFO JetspeedLoggingService -
Copy hofmann.jpeg to
/home/tomcats/t1/jakarta-tomcat-5.0.25/webapps/jetspeed/probusiness/empp
hotos/
So, from Windows to Windows and from a local position on the
Linix-Server the code
File file = new File(fileItem.getFileName());
String fileName= file.getName();
Results in a file name without path, but from windows to linux I get the
full path!
I searched in the bug database and found a similar problem under
TAPESTRY-127 UploadPart/getFileName().
But it seems that Jetspeed 1.5 does not use TAPESTRY.
I looked in the code of FileItem (turbine-2.3, because this is the
current download). There the file name is set in the constructor.
Any comments or help?
By the way the class FileUploader also has some problems:
It does not check for the correct file types (therefore I make a check
in my code). It also did not work under Linux (therefore the line:
hasUploaded = fu.upload(fileItem, path, fileTypes);
is commented out and I make the copy with my own code (see above).
Thomas Grundey
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]