posts - 325,  comments - 25,  trackbacks - 0

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.upload.FormFile;

public class UploadFile {
 private static String filePath = "/upload";

 @SuppressWarnings("deprecation")
 public static String buildDivContextFile(HttpServletRequest request,
   FormFile file, String id) throws FileNotFoundException, IOException {
  String fileName = "";
  filePath = request.getRealPath("/upload");
  synchronized (UploadFile.class) {
   File dirFile = new File(filePath);
   if (!dirFile.exists() || !dirFile.isDirectory()) {
    dirFile.mkdir();
   }
   int theIndex = 0;
   String fname = new String(file.getFileName().getBytes(), "utf-8");
   theIndex = fname.indexOf(".");
   String suffix = fname.substring(theIndex);
   fileName = id + suffix;
   InputStream streamIn = file.getInputStream();
   OutputStream streamOut = new FileOutputStream(filePath + "/"
     + fileName);
   int bytesRead = 0;
   byte[] buffer = new byte[8192];
   while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
    streamOut.write(buffer, 0, bytesRead);
   }
   streamOut.close();
   streamIn.close();
   file.destroy();
  }
  return fileName;
 }
}

posted on 2008-12-15 17:05 长春语林科技 阅读(134) 评论(0)  编辑  收藏 所属分类: struts

只有注册用户登录后才能发表评论。


网站导航:
 
<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

 

长春语林科技欢迎您!

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜