users@glassfish.java.net

unable to delete file?

From: <glassfish_at_javadesktop.org>
Date: Tue, 01 Jul 2008 17:43:07 PDT

i have a servlet that is getting a file via multipart/form-data and using com.oreilly.servlet.MultipartRequest to process. I get the file from the MultipartRequest copy it to another directory and then I want to delete the temp directory. NO GO. here is my code any help? (I have tried uploadedFile.delete(), I have tried new File(absfilepath).delete)<br>
<br>
I am not getting an error but File.delete returns false.
<br>
package com.zykis.www.Servlet;
<br>
import com.oreilly.servlet.MultipartRequest;<br>
import java.awt.Image;<br>
import java.io.*;<br>
import java.net.*;<br>
<br>
import java.util.Enumeration;<br>
import java.util.Properties;<br>
import javax.servlet.*;<br>
import javax.servlet.http.*;<br>
import javax.swing.ImageIcon;<br>
<br>
<br>
public class UploadServlet extends HttpServlet {<br>
 <br>
    private String tempPath;<br>
    private final int defaultMax = 20;<br>
    <br>
    public void init( ) {<br>
        tempPath = getServletContext( ).getRealPath("/") + "temp";<br>
    }<br>
    
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br>
    throws ServletException, IOException {<br>
       <br>
    try{ <br>
    MultipartRequest mpr = new MultipartRequest(
    request,tempPath,defaultMax * 1024 * 1024);<br>
    Enumeration fileEnum = mpr.getFileNames();<br>
    String fileIndex = (String) fileEnum.nextElement();<br>
    File uploadedFile = mpr.getFile(fileIndex); <br>
    <br>
    response.setContentType("text/html");<br>
    java.io.PrintWriter out = response.getWriter( );<br>
    out.println("<html>");<br>
    out.println("<head>");<br>
    out.println("<title>Servlet upload</title>");<br>
    out.println("</head>");<br>
    out.println("<body>");<br>
    <br>
    String dealerID = "";<br>
    String templateID = "";<br>
    String imageID = "";<br>
    boolean validRequest = true;<br>
    <br>
    if(mpr.getParameter("dealerID").compareToIgnoreCase("") != 0)<br>
    { dealerID = mpr.getParameter("dealerID"); }<br>
    else{ validRequest = false; } <br>
<br>
    if(validRequest && mpr.getParameter("templateID").compareToIgnoreCase("") != 0)<br>
    { templateID = mpr.getParameter("templateID"); }<br>
    else{ validRequest = false; } <br>
<br>
    if(validRequest && mpr.getParameter("imageID").compareToIgnoreCase("") != 0)
    { imageID = mpr.getParameter("imageID"); }<br>
    else{ validRequest = false; }<br>
    <br>
    if(uploadedFile == null)<br>
    { validRequest = false; }<br>
    <br>
    if(validRequest)<br>
    {<br>
        // create and load properties<br>
        Properties props = new Properties();<br>
        String fpath = request.getRealPath( "dealer" + dealerID +
                "/template" + templateID + "/upload.properties");<br>
        FileInputStream in = new FileInputStream(fpath);<br>
        props.load(in);<br>
        in.close();<br>
        <br>
        boolean validFile = true; <br>
        String fileName = uploadedFile.getAbsolutePath();<br>
        long fileSize = uploadedFile.length(); <br>
        String maxSize = props.getProperty("maxFileSize");<br>
        String validWidthString = props.getProperty("img" + imageID + "_w");<br>
        String validHeightString = props.getProperty("img" + imageID + "_h");<br>
        <br>
        int dot = fileName.indexOf(".");<br>
        String fileExt = fileName.substring(dot+1);<br>
        <br>
        //check file size<br>
        if(fileSize > Long.valueOf(maxSize))<br>
        { out.print("file is too big, max file size is " + maxSize);<br>
            validFile = false; <br>
        }<br>
        //check file type<br>
        if(validFile && props.getProperty(fileExt) == null) <br>
        { out.print(fileExt + " is an invalid file extension");<br>
            validFile = false; <br>
        }<br>
        //check width and height<br>
        Image img = new ImageIcon(fileName).getImage();<br>
        int imgWidth = img.getWidth(null);<br>
        int imgHeight = img.getHeight(null);<br>
        <br>
        if(Integer.valueOf(validWidthString) == 0)<br>
        { //DO NOTHING there is no restriction.<br>
        }<br>
        else if(imgWidth != Integer.valueOf(validWidthString))<br>
        { out.print("<br>width must equal " + validWidthString);<br>
            validFile = false; <br>
        }<br>
        <br>
        if(Integer.valueOf(validHeightString) == 0)<br>
        { //DO NOTHING there is no restriction.<br>
        }<br>
        if(imgHeight != Integer.valueOf(validHeightString))<br>
        { out.print("<br>height must equal " + validHeightString);<br>
            validFile = false;<br>
        }<br>
        <br>
        if(!validFile)<br>
        { uploadedFile.delete(); }<br>
        else<br>
        { <br>
            String newPath = getServletContext( ).getRealPath("dealer" <br>
                    + dealerID + "/template" + templateID <br>
                    + "/image" + imageID + "/" + uploadedFile.getName());<br>
            <br>
            File renamedFile = new File(newPath); <br>
            copyFile(uploadedFile, renamedFile); <br>
        }<br>
        //if not for specific template then add to dealers gallery<br>
    }else if(dealerID != null){<br>
                <br>
        String fileName = uploadedFile.getAbsolutePath(); <br>
        <br>
        Image img = new ImageIcon(fileName).getImage();<br>
        int imgWidth = img.getWidth(null);<br>
        int imgHeight = img.getHeight(null); <br>
                <br>
        File imgDir = new File(getServletContext().getRealPath("dealer" <br>
                + dealerID +"/Gallery/" + imgWidth + "x" + imgHeight));<br>
        <br>
        if(!imgDir.exists())<br><br>
        { imgDir.mkdirs(); }<br>
        <br>
        File newFile = new File(imgDir, uploadedFile.getName()); <br>
        copyFile(uploadedFile, newFile);<br>
        <br>
        out.println(newFile.getAbsolutePath());<br>
        out.println("file uploaded : " + uploadedFile.getAbsolutePath());<br>
    }else{<br>
        out.println("invalid request"); <br>
    } <br>
    <br>
    mpr.getFile(fileIndex).delete();<br>
    <br>
    out.println("</body>");<br>
    out.println("</html>"); <br>
    <br>
    }catch(Exception e)<br>
    { java.io.PrintWriter out = response.getWriter( );<br>
        out.println("<html>");<br>
        out.println("<head>");<br>
        out.println("<title>Servlet upload</title>");<br>
        out.println("</head>");<br>
        out.println("<body>");<br>
        out.print("Sorry there seems to be a problem with your upload<br>" + e.getMessage());<br>
        out.println("</body>");<br>
        out.println("</html>");<br>
    }<br>
    }<br>
    <br>
    private void copyFile(File oldFile, File newFile)throws Exception {<br>
        FileInputStream fis = new FileInputStream(oldFile);<br>
        FileOutputStream fos = new FileOutputStream(newFile);<br>
        byte[] buf = new byte[1024];<br>
        int i = 0;<br>
        while((i=fis.read(buf))!=-1) {<br>
            fos.write(buf, 0, i);<br>
        }<br>
        fis.close();<br>
        fos.close();<br>
    }<br>
[Message sent by forum member 'icemandrew' (icemandrew)]

http://forums.java.net/jive/thread.jspa?messageID=283836