日期:2014-05-16  浏览次数:20441 次

java上传文件 存至数据库处理方法
@Override
public PlanDoc[] buildPlanDoc(List<File> file,List<String> fileFileName){
PlanDoc[] planDoc = new PlanDoc[file.size()];
PlanDoc pd;
String docName;
String doceExtention = "";
//int maxFileSize = 2097152;  //默认配置最大为2兆,这里不必处理
FileInputStream fis = null;
for (int i = 0; i < file.size(); i++) {
//if(file.get(i).length() > 0 && file.get(i).length() < maxFileSize){
    int index = fileFileName.get(i).lastIndexOf('.');
    if (index != -1){
    docName = fileFileName.get(i).substring(0, index);
    doceExtention = fileFileName.get(i).substring(index);
    }else{
    docName = fileFileName.get(i);
    }
   
pd = new PlanDoc();
pd.setDocName(docName);
pd.setDoceExtention(doceExtention);
try {
byte[] buf = new byte[(int)file.get(i).length()];
fis = new FileInputStream(file.get(i));
fis.read(buf);
log.debug("##############################################读取文件字节长度为:" + buf.length);
pd.setDocu(buf);
} catch (FileNotFoundException e) {
throw new ServiceException("buildPlanDoc,上传文件时发生异常,未找到文件");
} catch (IOException e) {
throw new ServiceException("buildPlanDoc,上传文件时发生异常");
} finally {
try {
if (null != fis){
fis.close();
}
} catch (IOException e) {
throw new ServiceException("buildPlanDoc,上传文件时发生异常");
}
}

    planDoc[i] = pd;
//}else{
// throw new ServiceException("buildPlanDoc,上传文件过大,要求单个文件在5M以下");
//}
}
return planDoc;
}