【文件属性】:
文件名称:struts上传图片
文件大小:1.92MB
文件格式:RAR
更新时间:2012-08-28 13:20:18
struts 上传 图片
/*
* 使用java.util.Map接口实现文件组的上传
*/
private void muchUploadFile(IndexActionForm objForm) {
Map fileList = objForm.getFileList();
for(String str : fileList.keySet())
if((fileList.get(str)).getFileSize() > 0 && (fileList.get(str)).getFileSize() < BUFFER_SIZE) {
String fileName = DIRECTORY +"/"+ fileList.get(str).getFileName();
try {
this.fileStream(
fileList.get(str).getInputStream(), fileName, fileList.get(str).getFileSize());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
/*
* 使用org.apache.strtus.upload.FormFile实现文件的单一上传
*/
private void singleUploadFile(IndexActionForm objForm) {
if(objForm.getFile().getFileSize() == 0||
BUFFER_SIZE < objForm.getFile().getFileSize())
throw new RuntimeException("文件过大或不存在!!!");
String fileName = DIRECTORY +"/"+ objForm.getFile().getFileName();
try {
this.fileStream(
objForm.getFile().getInputStream(), fileName, objForm.getFile().getFileSize());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/*
* copy 到本地目录
*/
private void fileStream(InputStream strem, String fileName, int size) {
byte[] buffer = new byte[size];
try {
InputStream in = null;
OutputStream out = null;
try{
in = new BufferedInputStream(strem, size);
out = new BufferedOutputStream(new FileOutputStream(fileName), size);
while(in.read(buffer) > 0)
out.write(buffer);
}finally {
if(null != in)
in.close();
if(null != out){
out.flush();
out.close();
}
}
System.out.println("Uploading Success!!!");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
【文件预览】:
上传图片(Struts)
----StrutsUpload()
--------StrutsUpload()
----StrutsUpload.rar(982KB)