Struts2中将图片上传到服务器某一文件夹中

时间:2021-11-07 12:15:59
求助:Struts2中将图片上传到服务器某一文件夹中,如何实现?
    谢谢!!!

13 个解决方案

#1


你的问题没有提清楚
你是想上传到远程文件夹
还是你的程序跟文件夹都在同一个服务器上
如果是后者 
只需要用相对路径 就可以了吧

#2


就是正常的struts2上传!!

#3


程序跟文件夹都在同一个服务器上

#4


需要上传文件的包!
可以使用文件流读取形式上传!

#5


很简单啊,用struts的上传组件,google一下大把例子

#6


问题 解决了 吗 ,如果没解决的话 ,留个邮箱,我发给你

#7


谢谢,麻烦你发到这个邮箱weiyangkun@yahoo.cn

#8


jf                .

#9


action中
多个文件
//接受依赖注入的属性
private String savePath;
private String title;
//使用File数组封装多个文件域对应的文件内容
private List<File> upload;
//使用字符串数组封装多个文件域对应的文件类型
private List<String> uploadContentType;
//使用字符串数组封装多个文件域对应的文件名字
private List<String> uploadFileName;
@Override
public String execute() throws Exception {
// 取得需要上传的文件数组
List<File> files = getUpload();
// 遍历每个需要上传的文件
for (int i = 0; i < files.size(); i++) {
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName().get(i));
// 以每个需要上传的文件建立文件输入流
FileInputStream fis = new FileInputStream(files.get(i));
// 将每个需要上传的文件写到服务器对应的文件中
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
return SUCCESS;
}
一个文件
// 允许上传文件的类型
private String allowType;
// 直接在struts.xml文件中配置的属性
private String savePath;
// 封装文件标题请求参数的属性
private String title;
// 封装上传文件域的属性
private File upload;
// 封装上传文件类型的属性
private String uploadContentType;
// 封装上传文件名的属性
private String uploadFileName;

@Override
public String execute() throws Exception {
try {
String[] types = this.getAllowType().split(",");
if (!filterType(types)) {
ActionContext.getContext().put("typeError", "格式不正确!");
return INPUT;
}
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[2048];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
// 日志
throw new AuctionException("上传的文件格式不符或文件太大!");
}
return SUCCESS;
}

/**
 * 过滤文件类型
 * 
 * @param types
 * @return
 */
public boolean filterType(String[] types) throws Exception {
String fileType = this.getUploadContentType();
boolean boo = false;
for (String type : types) {
if (type.equalsIgnoreCase(fileType)) {
boo = true;
break;
}
}
return boo;
}

struts.xml中

<!-- 上传一个文件 -->
<action name="uploadAction"
class="org.agency.action.UploadAction">
<param name="allowType">image/pjpeg,image/bmp,image/gif,image/png,image/x-png</param>
<param name="savePath">/upload</param>
<result>/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>

<!-- 上传多个文件 -->
<action name="uploadAction2" class="org.agency.action.UploadAction2">
<!-- 配置fileUpload的拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型 -->
<param name="allowedTypes">image/pjpeg,image/bmp,image/gif,image/png,image/x-png</param>
<!-- 配置允许上传的文件大小 -->
<param name="maximumSize">2000000</param> 
</interceptor-ref> 
<!-- 配置系统默认的拦截器 -->
<interceptor-ref name="defaultStack"/>
<!-- 动态设置Action的属性值 -->
<param name="savePath">/upload</param>
<!-- 配置input逻辑视图对应的视图页面 -->
<result name="input">/upload2.jsp</result>
<!-- 配置Struts 2默认的视图页面 -->
<result>/success2.jsp</result>

#10


Action中定义一个属性
private String uploadDir;
getter/setter
...
Struts配置文件
<action ......>
    <param name="uploadDir">E:\\某个文件夹</param>
    ......
</action>
Action中上传时直接引用uploadDir作为目录即可,上传的代码网上一搜一大堆,何必在此作问?

#11


接分……

#12



设置上传的文件保存的路径为tomcat应用服务器的目录下,如当前工程下的upload文件夹
String uploadDir = "/upload" + dir;
upload为上传文件保存的文件夹,如果没有就在webroot下建一个upload文件名的文件夹

#13



不要设置绝对地址,这样就保存在客户机上了
设置相对的路径

#1


你的问题没有提清楚
你是想上传到远程文件夹
还是你的程序跟文件夹都在同一个服务器上
如果是后者 
只需要用相对路径 就可以了吧

#2


就是正常的struts2上传!!

#3


程序跟文件夹都在同一个服务器上

#4


需要上传文件的包!
可以使用文件流读取形式上传!

#5


很简单啊,用struts的上传组件,google一下大把例子

#6


问题 解决了 吗 ,如果没解决的话 ,留个邮箱,我发给你

#7


谢谢,麻烦你发到这个邮箱weiyangkun@yahoo.cn

#8


jf                .

#9


action中
多个文件
//接受依赖注入的属性
private String savePath;
private String title;
//使用File数组封装多个文件域对应的文件内容
private List<File> upload;
//使用字符串数组封装多个文件域对应的文件类型
private List<String> uploadContentType;
//使用字符串数组封装多个文件域对应的文件名字
private List<String> uploadFileName;
@Override
public String execute() throws Exception {
// 取得需要上传的文件数组
List<File> files = getUpload();
// 遍历每个需要上传的文件
for (int i = 0; i < files.size(); i++) {
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName().get(i));
// 以每个需要上传的文件建立文件输入流
FileInputStream fis = new FileInputStream(files.get(i));
// 将每个需要上传的文件写到服务器对应的文件中
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
return SUCCESS;
}
一个文件
// 允许上传文件的类型
private String allowType;
// 直接在struts.xml文件中配置的属性
private String savePath;
// 封装文件标题请求参数的属性
private String title;
// 封装上传文件域的属性
private File upload;
// 封装上传文件类型的属性
private String uploadContentType;
// 封装上传文件名的属性
private String uploadFileName;

@Override
public String execute() throws Exception {
try {
String[] types = this.getAllowType().split(",");
if (!filterType(types)) {
ActionContext.getContext().put("typeError", "格式不正确!");
return INPUT;
}
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[2048];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
// 日志
throw new AuctionException("上传的文件格式不符或文件太大!");
}
return SUCCESS;
}

/**
 * 过滤文件类型
 * 
 * @param types
 * @return
 */
public boolean filterType(String[] types) throws Exception {
String fileType = this.getUploadContentType();
boolean boo = false;
for (String type : types) {
if (type.equalsIgnoreCase(fileType)) {
boo = true;
break;
}
}
return boo;
}

struts.xml中

<!-- 上传一个文件 -->
<action name="uploadAction"
class="org.agency.action.UploadAction">
<param name="allowType">image/pjpeg,image/bmp,image/gif,image/png,image/x-png</param>
<param name="savePath">/upload</param>
<result>/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>

<!-- 上传多个文件 -->
<action name="uploadAction2" class="org.agency.action.UploadAction2">
<!-- 配置fileUpload的拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型 -->
<param name="allowedTypes">image/pjpeg,image/bmp,image/gif,image/png,image/x-png</param>
<!-- 配置允许上传的文件大小 -->
<param name="maximumSize">2000000</param> 
</interceptor-ref> 
<!-- 配置系统默认的拦截器 -->
<interceptor-ref name="defaultStack"/>
<!-- 动态设置Action的属性值 -->
<param name="savePath">/upload</param>
<!-- 配置input逻辑视图对应的视图页面 -->
<result name="input">/upload2.jsp</result>
<!-- 配置Struts 2默认的视图页面 -->
<result>/success2.jsp</result>

#10


Action中定义一个属性
private String uploadDir;
getter/setter
...
Struts配置文件
<action ......>
    <param name="uploadDir">E:\\某个文件夹</param>
    ......
</action>
Action中上传时直接引用uploadDir作为目录即可,上传的代码网上一搜一大堆,何必在此作问?

#11


接分……

#12



设置上传的文件保存的路径为tomcat应用服务器的目录下,如当前工程下的upload文件夹
String uploadDir = "/upload" + dir;
upload为上传文件保存的文件夹,如果没有就在webroot下建一个upload文件名的文件夹

#13



不要设置绝对地址,这样就保存在客户机上了
设置相对的路径