java web usefull coding

1.save
private File newsAttach;
private String newsAttachFileName;
private String newsAttachContentType;


if (newsAttach != null) {
String rp = request.getSession().getServletContext()
                        .getRealPath("/");
                String midPath = "uploadsrc/file/" + System.currentTimeMillis();
                String realPath = rp.replaceAll("\\\\", "/") + midPath + "/";

                FileUtil.savePic(realPath, getNewsAttachFileName(), newsAttach);

            }

2.download
String act= request.getParameter("act");
if (act.equals("downFile")) {
            String path = request.getParameter("path");
            String rp = request.getSession().getServletContext().getRealPath(
                    "/");
            rp = rp.substring(0, rp.length() - 1);

            File picFile = new File(rp + path);
            if (picFile != null && picFile.exists()) {
                response
                        .setContentType("application/octet-stream; charset=UTF-8");
                try {
                    response.setHeader("Content-Disposition",
                            "attachment;filename="
                                    + java.net.URLEncoder.encode(picFile
                                            .getName(), "UTF-8") + "");
                    OutputStream out = response.getOutputStream();
//need to import apache commons.io.jar pack
org.apache.commons.io.FileUtils.readFileToByteArray(picFile);
                    out.write(FileUtils.readFileToByteArray(picFile));
                    response.flushBuffer();
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

3.文delete
if (act.equals("delFile")) {
            String ret = "";
            String path = request.getParameter("path");
            String realRoot = request.getSession().getServletContext()
                    .getRealPath("/");
            realRoot = realRoot.substring(0, realRoot.length() - 1);
            boolean flag = FileUtil.delFile(realRoot+path);
            if (flag) {
                ret = "OK";
            }

            try {
                response.getWriter().write(ret);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;

        }

tools class

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

public class FileUtil {

    public static void saveFile(String RootPath,List<File> file,List<String> fileFileName) throws Exception{
        if (file != null) {
            int i = 0;
            for (; i < file.size(); i++) {
                java.io.InputStream is = new java.io.FileInputStream(file
                        .get(i));
                java.io.OutputStream os = new java.io.FileOutputStream(
                        RootPath + fileFileName.get(i));
                byte buffer[] = new byte[8192];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    os.write(buffer, 0, count);
                }
                os.close();
                is.close();
            }
        }
       
    }
   
    public static void savePic(String newsRootPath, String filename, File picFile) {
        try {
            File newsFileRoot = new File(newsRootPath);
            if (!newsFileRoot.exists()) {
                newsFileRoot.mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(newsRootPath + filename);
            FileInputStream fis = new FileInputStream(picFile);
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) > 0) {
                fos.write(buf, 0, len);
            }
            if (fis != null)
                fis.close();
            if (fos != null)
                fos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
   

    public static boolean delFile(String fpath) {
        boolean flag = false;
        File filePath = new File(fpath);
        if (filePath.exists()) {
            filePath.delete();
            flag = true;
        }
        return flag;
    }

   
}