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
长春语林科技 阅读(140)
评论(0) 编辑 收藏 所属分类:
struts