当前位置:首页 » 文件管理 » struts上传图片

struts上传图片

发布时间: 2024-12-07 03:09:13

A. Struts2中上传图片后如何让路径显示出来,

在action中定义一个变量,存储上传后的路径,生成setter,getter方法 ,jsp 中就可以直接拿到了

B. 在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++;
}
}

热点内容
vps自动脚本 发布:2025-03-15 20:50:29 浏览:59
php刷新重复提交 发布:2025-03-15 20:50:26 浏览:306
艾莫迅plc编程电缆 发布:2025-03-15 20:44:05 浏览:302
妖妖灵脚本 发布:2025-03-15 20:36:56 浏览:255
公司自己搭建ftp 发布:2025-03-15 20:36:07 浏览:61
如何增加配置使半袖变得不单调 发布:2025-03-15 20:33:37 浏览:349
linux显示目录 发布:2025-03-15 20:30:42 浏览:660
素数算法表示 发布:2025-03-15 20:24:02 浏览:842
大话西游手游怎么看服务器等级 发布:2025-03-15 20:21:53 浏览:222
rsa加密c源代码 发布:2025-03-15 19:53:55 浏览:693