當前位置:編程學習大全網 - 源碼下載 - java圖片批量上傳代碼?

java圖片批量上傳代碼?

用struts也可以實現 多文件上傳

下面是我寫的代碼,作為參考!

/*文件目錄*/

public static String [] fileArray={

"logo.png",

"index.swf",

"OEMInfo.txt",

"favicon.ico"};

/**

* @author Caoshun

* @see 接收並保存文件

* */

public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){

String fileName="";

//獲取表單中的文件資源

Hashtable<Object, Object> files = form.getMultipartRequestHandler().getFileElements();

//遍歷文件,並且循環保存

//當前處理文件序號

int file_num=1;

for (Enumeration<Object> e = files.keys(); e.hasMoreElements();) {

/*根據處理的當前文件下標,確定文件名*/

fileName=fileArray[file_num-1];

FormFile file = (FormFile) files.get((String) e.nextElement());

if (file != null && file.getFileSize() > 0) {

try {

//使用formfile.getInputStream()來獲取壹個文件的輸入流進行保存。

//文件名

//String fileName = file.getFileName();

//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);

//文件大小

//int fileSize = file.getFileSize();

//文件流

InputStream is = file.getInputStream();

//將輸入流保存到文件

//String rootPath = this.servlet.getServletContext().getRealPath("files");

//往cn中寫入

File rf = new File(rootPath1);

FileOutputStream fos = null;

fos = new FileOutputStream(new File(rf, fileName));

byte[] b = new byte[10240];

int real = 0;

real = is.read(b);

while (real > 0) {

fos.write(b, 0, real);

real = is.read(b);

}

//往en中寫入

File rf2 = new File(rootPath2);

InputStream is2 = file.getInputStream();

FileOutputStream fos2 = null;

fos2 = new FileOutputStream(new File(rf2, fileName));

byte[] b2 = new byte[10240];

int real2 = 0;

real2 = is2.read(b2);

while (real2 > 0) {

fos2.write(b2, 0, real2);

real2 = is2.read(b2);

}

//關閉文件流

fos.close();

is.close();

fos2.close();

is2.close();

} catch (RuntimeException e1) {

e1.printStackTrace();

} catch (Exception ee) {

ee.printStackTrace();

}

file.destroy();

}

file_num++;

}

}

  • 上一篇:求壹個VB小遊戲編程,要有代碼的。。
  • 下一篇:基坑工程設計采用的土的強度指標,應符合哪些規定?
  • copyright 2024編程學習大全網