ipu的trunk版的android工程和服务端工程。

FileUpDownload.java 6.2KB

    package com.ai.server.bean; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.ai.server.core.bean.DisplayBean; import com.ai.server.util.ApplicationPath; import com.ailk.common.data.IData; import com.ailk.mobile.servlet.ServletManager; /** * * @author Lu * */ public class FileUpDownload extends DisplayBean { /************************************************************************************ * 文件上传; * 如果在参数param中包含USER_NAME键值对,那么文件将保存到USER_NAME对应的目录下;否则保存在TEMP目录下 * * @param param * @return * @throws Exception */ public IData upload(IData param) throws Exception{ HttpServletRequest request = ServletManager.getRequest(); //获取文件需要上传到的路径 String path = ApplicationPath.getFilePath(request) + "upload" + File.separator; //获取临时目录,文件保存目录 String tempPath = path + "temp", savePath = null; File file = new File(tempPath); if(! file.exists() && ! file.mkdirs()){ throw new RuntimeException("生成上传临时文件夹失败,无法进行上传操作!"); } //获得磁盘文件条目工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //设置暂时存放的存储室,这个存储室可以和最终存储文件的目录不同 factory.setRepository(file); //设置缓存的大小,当上传文件的容量超过该缓存时,将产生临时文件并存储于临时目录中. factory.setSizeThreshold(5*1024*1024); //文件上传处理 ServletFileUpload upload = new ServletFileUpload(factory); try{ if (! ServletFileUpload.isMultipartContent(request)) { log.debug("获取的上传文件并非以文件流格式传送,请核查!"); return null; } List<?> fileList = upload.parseRequest(request); for(int i = 0; i < fileList.size(); i++){ FileItem item = (FileItem)fileList.get(i); //获取表单的属性名字 String name = item.getFieldName(); //如果获取的 表单信息是普通的文本信息 if(item.isFormField()){ //获取用户具体输入的字符串,因为表单提交过来的是字符串类型的 String value = item.getString(); request.setAttribute(name, value); //获取用户保存目录 if(savePath == null) savePath = "USER_NAME".equals(name) ? path + value : tempPath; }else{ //获取路径名 String value = item.getName(); int start = value.lastIndexOf("\\"); String filename = value.substring(start + 1); //保存文件名 request.setAttribute(name, filename); //检查保存目录 File saveDir = new File(savePath); if(! saveDir.exists() && ! saveDir.mkdirs()){ throw new RuntimeException("生成上传保存文件夹失败,无法进行上传操作!"); } //设置保存文件 File saveFile = new File(savePath, filename); //写到磁盘上 OutputStream out = new FileOutputStream(saveFile); InputStream in = item.getInputStream(); int length = 0; byte[] buf = new byte[1024]; //写出到文件中 while((length = in.read(buf) ) != -1) { //在BUF数组中取出数据写到(输出流)磁盘上 out.write(buf, 0, length); } out.flush(); in.close(); out.close(); log.debug("获取上传文件的总共的容量:" + item.getSize()); } } } catch (FileUploadException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } return null; } /************************************************************************************ * 文件下载; * * @param param * @return * @throws Exception */ public IData download(IData param) throws Exception{ HttpServletRequest request = ServletManager.getRequest(); HttpServletResponse response = ServletManager.getResponse(); //获取文件需要上传到的路径 String fileName = request.getParameter("FILE_PATH"); String mineType = request.getParameter("MINE_TYPE"); log.debug("MINE类型为:[" + mineType + "]"); // log.debug(request.getParameter("USER_NAME")); // log.debug(request.getParameter("SESSION_ID")); //获取文件地址 String filePath = ApplicationPath.getFilePath(request) + "upload" + File.separator + fileName; File file = new File(filePath); if(! file.isFile()) throw new RuntimeException("上传的文件不存在,请检查文件路径!"); response.reset(); response.setContentType(mineType); InputStream in = new FileInputStream(file); int length = 0; byte[] buf = new byte[1024]; //写出到文件中 OutputStream out = response.getOutputStream(); while((length = in.read(buf) ) != -1) { //在BUF数组中取出数据写到(输出流)磁盘上 out.write(buf, 0, length); } out.flush(); in.close(); out.close(); response.flushBuffer(); return null; } }