阅读背景:

java不能删除文件的解决办法

来源:互联网 
public void download(String path,String contentType,HttpServletRequest request,HttpServletResponse response) throws IOException {  
		InputStream fis = null;
		OutputStream out = null;   
		File file=null;
		try {
			if(contentType.equals("pdf")){
				contentType = "application/pdf";
			}else if(contentType.equals("doc")){
				contentType = "application/octet-stream";
			}
			file=new File(path);
			fis=new BufferedInputStream(new FileInputStream(path)); 
			String filename = path.substring(path.lastIndexOf("/")+1,path.length());
			filename = "关于"+filename.substring(filename.lastIndexOf("@")+1,filename.length());
			filename = new String(filename .getBytes(), "ISO-8859-1");
			response.setContentType(contentType);
	        response.setCharacterEncoding ("utf-8");//设置编码集,文件名不会发生中文乱码  
	        response.setHeader ("Content-Disposition", "attachment;filename=" + filename);  
	        out = response.getOutputStream();
	        // 以流的形式下载文件。
	        byte[] buffer = new byte[fis.available()];
	        fis.read(buffer);
	        out.write(buffer);
	        out.flush ();  
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fis!=null){
				try {
					fis.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close ();  
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			if(file.exists() && file.isFile()){
				try {
					System.gc();
					if(file.delete()){
						System.out.println("成功删除文件");
					}else{
						System.out.println("删除文件失败");
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		} 
	}public void download(String path,String content



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: