Project

General

Profile

« Previous | Next » 

Revision 7823

use standard File.createTempFile() method for uploaded data files and delete them when we are done with them. https://projects.ecoinformatics.org/ecoinfo/issues/6008

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
2651 2651
        Logger logMetacat = Logger.getLogger(MetacatHandler.class);
2652 2652
        PrintWriter out = null;
2653 2653
        String action = null;
2654
        File tempFile = null;
2654 2655
        
2655 2656
        // Parse the multipart form, and save the parameters in a Hashtable and
2656 2657
        // save the FileParts in a hashtable
......
2692 2693
                    // it's a file part
2693 2694
                    FilePart filePart = (FilePart) part;
2694 2695
                    String fileName = filePart.getFileName();
2695
                    String fileTempLocation;
2696 2696
                    
2697 2697
                    // the filePart will be clobbered on the next loop, save to disk
2698
                    fileTempLocation = MetacatUtil.writeTempUploadFile(filePart, fileName);
2699
                    fileList.put(name, fileTempLocation);
2698
                    tempFile = MetacatUtil.writeTempUploadFile(filePart, fileName);
2699
                    fileList.put(name, tempFile.getAbsolutePath());
2700 2700
                    fileList.put("filename", fileName);
2701
                    fileList.put("name", fileTempLocation);
2701
                    fileList.put("name", tempFile.getAbsolutePath());
2702 2702
                } else {
2703 2703
                    logMetacat.info("MetacatHandler.handleMultipartForm - " +
2704 2704
                    		        "Upload name '" + name + "' was empty.");
......
2787 2787
            out.println("Error: action not registered.  Please report this error.");
2788 2788
            out.println("</error>");
2789 2789
        }
2790
        
2791
        // clean up the temp file
2792
        if (tempFile != null && tempFile.exists()) {
2793
        	tempFile.delete();
2794
        }
2790 2795
        out.close();
2791 2796
    }
2792 2797
    
src/edu/ucsb/nceas/metacat/util/MetacatUtil.java
400 400
	 *            the name of the file to be written to disk
401 401
	 * @return tempFilePath a String containing location of temporary file
402 402
	 */
403
    public static String writeTempUploadFile (FilePart filePart, String fileName) throws IOException {
404
        String tempFilePath = null;
403
    public static File writeTempUploadFile (FilePart filePart, String fileName) throws IOException {
404
        File tempFile = null;
405 405
        String tempDirPath = null;
406 406
        try {
407 407
        	tempDirPath = PropertyService.getProperty("application.tempDir") + FileUtil.getFS() + "uploads";
......
410 410
        			+ "to use system temp directory: " + pnfe.getMessage());
411 411
        }
412 412
        long fileSize;
413
        File tempFile;
414 413
        File tempDir;
415 414

  
416 415
        if ((fileName == null) || fileName.equals("")) {
417
            return tempFilePath;
416
            return tempFile;
418 417
        }
419 418
				
420 419
        // the tempfilepath token isn't set, use Java default
......
440 439
					+ e.getMessage());
441 440
		}
442 441

  
443
		tempFile = new File(tempDirPath, fileName);
442
		//tempFile = new File(tempDirPath, fileName);
443
		tempFile = File.createTempFile("upload", ".tmp", tempDir);
444 444
		fileSize = filePart.writeTo(tempFile);
445
		tempFilePath = tempDirPath + File.separator + fileName;
446 445

  
447 446
		if (fileSize == 0) {
448 447
			logMetacat.warn("Uploaded file '" + fileName + "'is empty!");
449 448
		}
450 449

  
451
        logMetacat.debug("Temporary file is: " + tempFilePath);
450
        logMetacat.debug("Temporary file is: " + tempFile.getAbsolutePath());
452 451

  
453
        return tempFilePath;
452
        return tempFile;
454 453
    }
455 454

  
456 455
    /**

Also available in: Unified diff