struts 中实现文件的上传

------------文件上传
java 代码
  1. public class UploadFile {      
  2.     public static String upLoad(FormFile file,String uploadDir,String newName) throws FileNotFoundException, IOException{      
  3.         String data = null;      
  4.         String location = null;      
  5.         String totalname;      
  6.         String extend;      
  7.         StringBuffer fileName;      
  8.         String contentType;      
  9.         String size;      
  10.               
  11.         int i;      
  12.         File dirPath;      
  13.         InputStream inputStream;      
  14.         OutputStream outputStream;      
  15.               
  16.         //得到上传文件的全名,包括扩展名      
  17.         totalname = file.getFileName();                
  18.          //得到扩展名      
  19.         i = totalname.lastIndexOf(".");      
  20.                
  21.          //如果没有上传图片,则返回空      
  22.          if(i==-1){      
  23.             return location;      
  24.          }      
  25.                   
  26.          extend = totalname.substring(i);      
  27.          //重命名该文件      
  28.          fileName = new StringBuffer(newName + extend);      
  29.          fileName.toString();           
  30.               
  31.         //得到文件类型      
  32.         contentType = file.getContentType();      
  33.         //得到文件大小      
  34.         size = (file.getFileSize() + " bytes");      
  35.      
  36.         dirPath = new File(uploadDir);      
  37.         if (!dirPath.exists()) {      
  38.             dirPath.mkdirs();      
  39.         }      
  40.      
  41.         inputStream = file.getInputStream();      
  42.         outputStream = new FileOutputStream(uploadDir + fileName);      
  43.         int bytesRead = 0;      
  44.         byte[] buffer = new byte[8192];      
  45.      
  46.         while ((bytesRead = inputStream.read(buffer, 08192)) != -1) {      
  47.             outputStream.write(buffer, 0, bytesRead);      
  48.         }      
  49.      
  50.         location = dirPath.getAbsolutePath()      
  51.             + System.getProperty("file.separator") + fileName + "\"";      
  52.               
  53.         outputStream.close();      
  54.         inputStream.close();      
  55.         file.destroy();      
  56.               
  57.         return location;      
  58.     }      
  59. }      

 

--------------------文件的下载

java 代码
  1. public ActionForward execute(ActionMapping mapping, ActionForm form,   
  2.    HttpServletRequest request, HttpServletResponse response) {   
  3.   String strFileName = "测试文件.rar";   
  4.   File file = new File("具体路径" + strFileName);//   
  5.   if(file.exists()){   
  6.    try{   
  7.     BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));   
  8.     byte[] buffer = new byte[1024];   
  9.     strFileName = java.net.URLEncoder.encode(strFileName, "UTF-8");//处理中文文件名的问题   
  10.     strFileName = new String(strFileName.getBytes("UTF-8"),"GBK");//处理中文文件名的问题   
  11.     response.reset();   
  12.     response.setCharacterEncoding("UTF-8");   
  13.     response.setContentType("application/x-rar-compressed");//不同类型的文件对应不同的MIME类型   
  14.     response.setHeader("Content-Disposition","attachment; filename=" + strFileName);   
  15.     OutputStream os = response.getOutputStream();   
  16.     while(bis.read(buffer) > 0){   
  17.      os.write(buffer);   
  18.     }   
  19.     bis.close();   
  20.     os.close();   
  21.    }   
  22.    catch(Exception e){   
  23.     ......   
  24.    }   
  25.   }   
  26.   return mapping.getInputForward();   
  27.  }   
  28.   

你可能感兴趣的:(.net,struts,OS)