AGMedlem sedan mars 20001 266 inlägg Följande kod använder jag för att hitta och kopiera en fil till en speciell katalog.
<%@ page import="java.io.*" %>
<%
// File (or directory) to be moved
File file = new File("c:/dir/banner2.bmp");
// Destination directory
File dir = new File("c:/dir/banner");
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
System.out.println("File was not successfully moved");
// File was not successfully moved
}
else{
System.out.println("File was successfully moved");
}
%>
Dock skulle jag vilja slippa, eller rättare sagt, måste slippa att behöva ange en hel sökväg, (c:/...).
Jag vill att programmet skall utgå från den katalogen programmet ligger i. Dvs banner2.bmp ligger i samma katalog som denna jspsida, och katalogen banner är en katalog i denna katalog.
Tack
Andreas
ViktorMedlem sedan aug. 20021 752 inlägg Du kan använda "pageContext.getServletContext().getRealPath("")" för att få sökvägen till din JSP sida.
<%@ page import="java.io.*" %>
<%
// File (or directory) to be moved
File file = new File(pageContext.getServletContext().getRealPath("")+"/space.gif");
// Destination directory
File dir = new File(pageContext.getServletContext().getRealPath("")+"/jffc/");
// Move file to new directory
if(!file.renameTo(new File(dir, file.getName())))
out.println("File was not successfully moved");
else
out.println("File was successfully moved");
%>
Flyttar tråden till J2EE.
/Viktor
AGMedlem sedan mars 20001 266 inlägg Upptäckte att detta flyttade hela filen. Skulle endast vilja att den kopierade filen, och byta namn på den i den nya katalogen.
Tack
Ändreas
ViktorMedlem sedan aug. 20021 752 inlägg Då får du skapa den nya filen och sedan kopiera över innehållet i den gammla filen till den nya.
<%@ page import="java.io.*" %>
<%
FileInputStream fis = new FileInputStream(new File(pageContext.getServletContext().getRealPath("")+"/space.gif"));
FileOutputStream fos = new FileOutputStream(new File(pageContext.getServletContext().getRealPath("")+"/jffc/MY_NEW_NAME.gif"));
byte[] buf = new byte[1024];
int i = 0;
while((i=fis.read(buf))!=-1)
{
fos.write(buf, 0, i);
}
fis.close();
fos.close();
%>
Koden är testad och fungerar :)
/Viktor
AGMedlem sedan mars 20001 266 inlägg Tack, men jag hittade en annan lösning, fungerar som nedan.
'copyFile.java
import java.io.*;
import java.util.*;
public class copyFile {
public boolean copyFile(String source,String target) throws IOException
{
File filesource=new File(source);
if(filesource!=null)
{
if(filesource.exists()&&filesource.isFile())
{
File filetarget=new File(target);
if(filetarget!=null)
{
filetarget.createNewFile();
if(filetarget.exists()&&filetarget.isFile())
{
byte [] data;
FileInputStream fis = new FileInputStream(filesource);
FileOutputStream fos=new FileOutputStream(filetarget);
data=new byte[fis.available()];
while(fis.read(data)>0)
{
fos.write(data);
data=new byte[fis.available()];
}
fis.close();
fos.close();
}
else throw new IOException(target+" is not a file");
}
else throw new IOException("Can't instatiate class File with String parameter "+target);
}
else throw new IOException(source+" is not a file");
}
else throw new IOException("Can't instatiate class File with String parameter "+source);
return(true);
}
}
'jspsidan
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="copyFile" %>
<%
// File (or directory) to be moved
String source = pageContext.getServletContext().getRealPath("")+"img/banner2.bmp";
String target = pageContext.getServletContext().getRealPath("")+"pub/banner.bmp";
// Move file to new directory
copyFile cf = new copyFile();
if(!cf.copyFile(source,target)){
System.out.println("Failed in creation of banner");
out.println("Failed in creation of banner");
}
else{
System.out.println("Banner uploaded");
out.println("Banner uploaded");
}
%>
Andreas