spring 多文件上传

 /**
     *  招标多文件上传
     * @author zhanglong
     * @param req
     * @param bidorg
     *                招标机构编号
     * @param type
     *                文档类别
     * @param fName
     *                文件域名称
     * @return
     * @throws Exception
     */
    
    public List<DataFile>  uploadGNZB(HttpServletRequest req, String bidorg, String type,String fName)throws Exception {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)req;
    //得到所有的文件
    List<MultipartFile>   files=new ArrayList<MultipartFile>();
    //根据文件参数名得到文件并且他添加到集合中
    for (Iterator  it = multipartRequest.getFileNames(); it.hasNext();) {  
           String key = (String)it.next();  
            MultipartFile file = multipartRequest.getFile(key);  
           if (file.getOriginalFilename().length() > 0) {  
             files.add(file);  
          }  
    }  
    DataFile uf = null;
    InputStream is = null;
    MultipartFile   file=null;
     
    //保留用的(这里是否要返回List<DataFile>)
    List<DataFile>    ufs=new  ArrayList<DataFile>();
    
    for (int i = 0; i < files.size(); i++) {
    file=files.get(i);
       String contenttype = file.getContentType();
       String filename = getName(file.getOriginalFilename());// .getName());
       long length = file.getSize();
       String filepath = bidorg + "/" + getNextFileName();
       is = file.getInputStream();
       LogMgr.bizlog
       .warn("rootPath:" + rootPath + ",filepath:" + filepath);
       long result = writeToFile(is, rootPath + filepath);
       if (result > 0) {
    uf = new DataFile();
    uf.setBidorg(bidorg);
    uf.setContentType(contenttype);
    uf.setFileName(filename);
    uf.setLength(length);
    uf.setPath(filepath);
    uf.setType(type);
    uf.setGentime(new Date());
    save(uf);
    ufs.add(uf);
       }
  
}
    LogMgr.bizlog.info("文件上传成功**************************:");
 
    return ufs;
}


    
    /**
     * 验证多文件上传文件内容不能为空
     * @param req
     * @param fName
     * @param fileSize 限制的文件大小
     */
@Override
public boolean validionFiles(HttpServletRequest req, String fName)
throws Exception {
// TODO Auto-generated method stub
return false;
}

    /**
     * 验证多文件上传文件
     * @param req
     * @param fName
     * @param fileSize 限制的文件大小
     */
@Override
public boolean validionFiles(HttpServletRequest req, String fName,
long fileSize) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)req;
    //得到所有的文件
    List<MultipartFile>   files=new ArrayList<MultipartFile>();
    //根据文件参数名得到文件并且他添加到集合中
    boolean  flag=true;
    for (Iterator  it = multipartRequest.getFileNames(); it.hasNext();) {  
           String key = (String)it.next();  
            MultipartFile file = multipartRequest.getFile(key);  
            if (file == null) 
        throw new Exception("无效请求参数");
        if(file.getSize()>fileSize)
        flag = false; 
    } 
    return flag; 
}


/**
* 针对不同的供应商文件多文件上传
*/
 
public List<DataFile>  uploadProvinders(HttpServletRequest req, String bidorg, String type,String fName)throws Exception {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)req;
    //得到所有的文件
    List<MultipartFile>   files=new ArrayList<MultipartFile>();
    //根据文件参数名得到文件并且他添加到集合中
    //String  WJ="zbwj";
    for (Iterator  it = multipartRequest.getFileNames(); it.hasNext();) { 
    
    //int  i=0;
    //根据供应商去判读是否是该供应商
           String key= (String)it.next();
           String  keyW=key.substring(5);
           String  keyQ=type.substring(4);
          
           if(keyW.equalsIgnoreCase(keyQ)){
           MultipartFile file = multipartRequest.getFile(key);  
            if (file.getOriginalFilename().length() > 0) {  
            
files.add(file);  
            } 
           
           }
          
         
    }  
    DataFile uf = null;
    InputStream is = null;
    MultipartFile   file=null;
    
    //保留用的(这里是否要返回List<DataFile>)
    List<DataFile>    ufs=new  ArrayList<DataFile>();
    
    for (int i = 0; i < files.size(); i++) {
    file=files.get(i);
       String contenttype = file.getContentType();
       String filename = getName(file.getOriginalFilename());// .getName());
       long length = file.getSize();
       String filepath = bidorg + "/" + getNextFileName();
       is = file.getInputStream();
       LogMgr.bizlog
       .warn("rootPath:" + rootPath + ",filepath:" + filepath);
       long result = writeToFile(is, rootPath + filepath);
       if (result > 0) {
    uf = new DataFile();
    uf.setBidorg(bidorg);
    uf.setContentType(contenttype);
    uf.setFileName(filename);
    uf.setLength(length);
    uf.setPath(filepath);
    uf.setType(type);
    uf.setGentime(new Date());
    save(uf);
    ufs.add(uf);
       }

}
    LogMgr.bizlog.info("文件上传成功**************************:");
 
    return ufs;
}



----------------------------------------------------------------------------------------------------------------------------------------------

   /**
     * 获取文件所在的全物理路径
     * 
     * @param fileId
     * @return
     * @throws Exception
     */
    public String getFilePath(String fileId) throws Exception {
return rootPath + getFile(fileId).getPath();
    }


    public long writeToFile(InputStream is, String filename) {
FileOutputStream fos = null;
byte[] buff = new byte[65536];
long filelength = 0;
try {
   File file = new File(filename);
   LogMgr.bizlog.warn("filename:" + filename);
   file.getParentFile().mkdirs();
   fos = new FileOutputStream(filename);
   int len = is.read(buff);
   while (len > 0) {
fos.write(buff, 0, len);
filelength += len;
len = is.read(buff);
   }
} catch (Exception e) {
   e.printStackTrace();
   LogMgr.bizlog.error("写入文件失败***************************:"
   + e.getMessage() + " e.getCause():" + e.getCause());
   return 0;


} finally {
   try {
if (fos != null) {
   fos.close();
   fos = null;
}
if (is != null) {
   is.close();
   is = null;
}


   } catch (Exception e) {
e.printStackTrace();
LogMgr.bizlog.error("写入文件失败***************************:"
+ e.getMessage() + " e.getCause():" + e.getCause());
   }
}
return filelength;
    }

--------------------------------------

public List find(String sQuery, int start, int size, List values) {
   List ret = new ArrayList();
   Session session = getsession();
//    try{  
Query query = session.createQuery(sQuery);
if (values != null && values.size() > 0) {

for (int i = 0, isize = values.size(); i < isize; i ++) {
query.setParameter(i, values.get(i));
}
}
if (start > 0)
query.setFirstResult(start);
if (size > 0)
query.setMaxResults(size);
ret = query.list();

你可能感兴趣的:(spring 多文件上传)