当前位置: 代码迷 >> Web前端 >> java Web惯用基本操作代码
  详细解决方案

java Web惯用基本操作代码

热度:291   发布时间:2012-09-21 15:47:26.0
java Web常用基本操作代码
1.Struts2中文件保存

//设置文件的三个属性,并且提供set方法 (此处省略)
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.在Struts2中的文件下载

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();
//需要导入apache的commons.io.jar包
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.文件删除
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;

		}



工具类:
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;
	}

	
}

  相关解决方案