圖片上傳

 第一步: jsp

批量圖片導入:<input id="fileimg" type="file" name="fileimg"  >

  <input id="submitImage" type="submit" value=" 提交 ">

第二步:Action 類

private File fileimg;

private static boolean excelLock = false;

private final static String filePre = "/data/homes/ftesitemanage/temp/";

/**

*     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;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章