第一步: jspapache
批量圖片導入:<input id="fileimg" type="file" name="fileimg" >服務器
<input id="submitImage" type="submit" value=" 提交 ">jsp
第二步:Action 類ide
private File fileimg;oop
private static boolean excelLock = false;this
private final static String filePre = "/data/homes/ftesitemanage/temp/";url
/**spa
* excel 導入excel
* @return圖片
*/
public String import_excel(){
if (excelLock) return null;
try {
excelLock = true;
String activityId=getParameter("excelActivityid");
String filePath = "";
File newFile=null;
filePath = filePre+System.currentTimeMillis();
if("excel".equals(fileType)){
newFile = new File( filePath+".xls");
FileUtils.copyFile(file, newFile);
if(fileimg!=null){
newFile=new File(filePre);
if(!newFile.exists()){
FileUtil.makeParent(newFile);
}
derExcute.unZipFile(fileimg,filePre);
}
if(!"".equals(cooperationService.importExcelCooperation(activityId, filePath+".xls"))){ //說明有分配新短號失敗的經紀人
this.setWriterPrint("<script>parent.showfailSpan('0');</script>");
return null;
}else{
this.setWriterPrint("<script>parent.showfailSpan('1');</script>");
return null;
}
}
} catch (Exception e){
e.printStackTrace();
}finally {
excelLock = false;
}
return null;
}
第三步: ServiceImpl 處理類
import org.apache.commons.io.FileUtils;
public String validateUrlToLocal(String str,String filePath) {
if (str.indexOf("pic.homescn.com") > 0) {//服務器上的圖片,就直接剪切對應的地址
return str.substring(str.indexOf(".com/") + 5);
}else{//若是文本直接就是圖片名稱: 就加上文件路徑
String localImg=filePre+str;
String httpurl=doImgPath();
try {
FileUtils.copyFile(new File(localImg),new File(httpurl)); //上傳圖片到指定服務器地址
str=httpurl.substring(httpurl.indexOf("ftepic/")+7);
} catch (IOException e) {
System.out.println("上傳圖片異常: "+e.getLocalizedMessage());
}
}
//把圖片下載到本地
// String filename = str.substring(str.lastIndexOf("/"));
// WebUtil.saveUrlToLocal(localPath + filename, str);
return str;
}