当前位置:首页 » 文件管理 » struts2上传路径

struts2上传路径

发布时间: 2022-11-21 15:21:53

❶ struts2 文件上传绝对路径

ServletActionContext.getServletContext().getRealPath("/projectName");

❷ struts 2 上传文件时,怎么样让它的默认路径是WebContent工程下的目录

你这样写是没错的,只是用eclipse开发时,eclipse会自己产生一个暂时目录将你的web项目发布到该暂时目录下。你可以看下那个暂时目录,结构和你的工程目录是一样的。
eclipse的工作目录和测试用的目录是不一样的

❸ struts2上传文件放到web目录下

你用的是struts2的话可以直接把上传路径配在xml里面,比如
<action name="uploadFile" class="json" method="uploadFile">
<param name="savePath">/upload</param>
</action>
然后再在action类里面设置一个名为savePath的成员变量,然后创建一组setter和getter
你要的路径就可以用getSavePath()得到了。
这个路径就是在里的工程文件夹下的upload文件夹里面

❹ struts2 文件上传怎么指定保存文件的路径和大小

在action配置文件struts.xml里设置(如下):
<package name="upload" extends="struts-default">
<action name="upload" class="">
<!--配置fieldUpload拦截器--->
<interceptor-ref name="fileUpoad">
<param name="allowedTypes">image/bmp,image/png,image/jpg,image/gif</param>
<param name="maximumSize">200000</param>
</interceptor-ref>
<!---必须显示配置引用struts默认的拦截器栈:defaultStack----->
<interceptor name="defaultStack"></interceptor>
<!---设置上传路径----->
<param name="savePath">/upload</param>
<result name="success">/upload_succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
希望能帮到你哈....

❺ struts2 文件上传路径错误 ERROR [STDERR] java.io.FileNotFoundException:

这个可能就是你的Struts2的配置路径的原因了,你在struts.xml中配置这个Struts的路径的时候,可能多加了一个“.”,因此在路径中才会有“.”出现的原因吧,所以你检查一下你的配置文件看看

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

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

❼ struts2怎么改变文件上传地址

所谓上传,其实就是文件的拷贝,涉及到流的读写,上传的路径可以自己定义的,另外struts2可以在struts.xml中定义临时存储上传文件的路径,如下:
<!-- 系统常量定义,定义上传文件临时存放路径 -->
<constant name="struts.multipart.saveDir" value="d:\"></constant>
至于文件上传的路径,一般是获取到的上下文路径,当然也可以是别的,如下:
//上传文件存放路径,如果不存在,可以创建
private final static String UPLOADDIR = "/upload";
//执行上传功能
private void uploadFile(int i) throws FileNotFoundException, IOException {
try {
InputStream in = new FileInputStream(file.get(i));
//获取上下文路径
String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);
//写文件
File uploadFile = new File(dir, this.getFileFileName().get(i));
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
最后,在struts.xml中添加
<!-- 系统常量定义,定义上传文件字符编码集 -->
<constant name="struts.i18n.encoding" value="gb2312"></constant>
避免编码出现问题,另外还有文件大小、类型等等限制,网上有好多例子,可以参考一下,不知道这些对你有没有用

❽ 为什么struts2上传文件自动保存到tomcat下,如何更改成自定义路径

1:上传的路径是可以自定义的
2:上传需要的jar包:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
3:直接上主要代码:
(1)jsp页面:
<form action="./user/upload.action" method="post" enctype="multipart/form-data">
上传文件:<input type="file" name="upload"><br>
<input type="submit" value="上传">
</form>
(2)struts配置:
<package name="example" namespace="/user" extends="struts-default">
<action name="upload" class="com.struts2.action.UploadAction" method="upload">
<result name="success" type="dispatcher">/example/success.jsp</result>
</action>
</package>
(3)java代码:
public class UploadAction {
private File upload;
private String uploadFileName ; //格式 <input name="upload"> + FileName ;
private String uploadContentType ; //格式<input name="upload"> + ContentType ;
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String upload() throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(upload));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/temp/"+uploadFileName));
byte[] b = new byte[1024*1024];
int i = bis.read(b);
while(i!=-1){
bos.write(b,0,i);
i = bis.read(b);
}
bos.flush();
bis.close();
bos.close();
return "success" ;
}

❾ struts2在linux下上传文件路径的问题

很有可能是权限不对吧, 那个目录是任何人都可以读写吗? chmod 777 **/tomcat/webapps/soft 这样试试,看行不行

❿ struts2.0怎么实现上传文件

一、创建jsp页面:
注意!要上传文件,表单必须添加 enctype 属性,如下: enctype="multipart/form-data"
index.jsp 代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 注意!表单必须添加 enctype 属性,值为"multipart/form-data" -->
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传"/>
</form>
</body>
</html>

二、创建Action类:
1. 添加三个私有字段,并添加相应的get,set方法。
private File file; ——上传的文件,变量名对应页面上"file"input的name属性值。类型为java.io.File
private String fileContentType;——上传文件的格式类型名,变量名格式为:页面上"file"input的name属性值+ContentType
private String fileFileName——上传的文件名,变量名格式为:页面上"file"input的name属性值+fileFileName。
2. 使用struts2提供的FileUtils类拷贝进行文件的拷贝。FileUtils类位于org.apache.commons.io包下。
3. 在项目目录下的WebContent目录下添加 upload 文件夹,用于存放客户端上传过来的文件,对应第15行代码。
Upload.java代码如下:

1 import java.io.File;
2 import java.io.IOException;
3 import org.apache.commons.io.FileUtils;
4 import org.apache.struts2.ServletActionContext;
5 import com.opensymphony.xwork2.ActionSupport;
6
7 public class Upload extends ActionSupport{
8 private File file;
9 private String fileContentType;
10 private String fileFileName;
11
12 @Override
13 public String execute() throws Exception {
14 //得到上传文件在服务器的路径加文件名
15 String target=ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName);
16 //获得上传的文件
17 File targetFile=new File(target);
18 //通过struts2提供的FileUtils类拷贝
19 try {
20 FileUtils.File(file, targetFile);
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24 return SUCCESS;
25 }
26
27 //省略get,set方法...........
28
29 }

三、在struts.xml中添加相应的配置代码。
struts.xml代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="Upload">
<result>index.jsp</result>
</action>
</package>
</struts>

四、测试。
启动服务器,进入index页面。

选择一改图片,点击上传提交表单。

打开upload文件夹(注意,这里指的是web服务器下的目录,如我用的web服务器是tomcat安装在电脑D盘,项目名称为“Struts2Upload”那么其路径为:D:\apache-tomcat-7.0.40\webapps\Struts2Upload\upload)可以看到刚才选中的图片已经上传到该目录下了。

上传多个文件
一、修改页面文件
增加继续添加按钮和 addfile() 方法,让页面可以通过javascript增加 input 标签。
修改后的 index.jsp代码如下:

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <script type="text/javascript">
8 //添加javascript方法 addfile() 在页面中境加input标签、
9 function addfile(){
10 var file = document.createElement("input");
11 file.type="file";
12 file.name="file";
13 document.getElementById("fileList").appendChild(file);
14 document.getElementById("fileList").appendChild(document.createElement("br"));
15 }
16 </script>
17 <title>Insert title here</title>
18 </head>
19 <body>
20 <!-- 注意!表单必须添加 enctype 属性,值为"multipart/form-data" -->
21 <form action="upload.action" method="post" enctype="multipart/form-data">
22 <div id="fileList">
23 <input type="file" name="file" /><br/>
24 </div>
25 <!-- 添加继续添加按钮,点击按钮调用addfile() -->
26 <input type="button" value="继续添加" onclick="addfile()" />
27 <input type="submit" value="上传"/>
28 </form>
29 </body>
30 </html>

二、修改Action类
1. 把三个私有字段(file,fileContentType,fileFileName)的类型更改为数组或集合类型。并添加相应的get,set方法。
2. 通过循环来上传多个文件。
修改后的Upload.java代码如下:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Upload extends ActionSupport{
//把这三个字段类型更改为数组或集合
private File[] file;
private String[] fileContentType;
private String[] fileFileName;

@Override
public String execute() throws Exception {
//通过循环来上传多个文件
for(int i=0;i<file.length;i++){
//得到上传文件在服务器的路径加文件名
String target=ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName[i]);
//获得上传的文件
File targetFile=new File(target);
//通过struts2提供的FileUtils类拷贝
try {
FileUtils.File(file[i], targetFile);
} catch (IOException e) {
e.printStackTrace();
}
}
return SUCCESS;

}

//省略set,get方法...................

}

三、测试
1. 启动服务器,打开index.jsp页面。点击继续添加,添加两个input。同时上传三张图片。

2. 点击上传提交表单。打开upload文件夹,可以看到刚才选中的三张图片已经上传到该目录下了。

参考资料http://www.cnblogs.com/likailan/p/3330465.html

热点内容
黑莓加密天线 发布:2024-10-05 18:30:07 浏览:848
编程入行年龄 发布:2024-10-05 18:29:24 浏览:537
服务器地址访问不到 发布:2024-10-05 18:20:55 浏览:688
手机解锁忘记密码多少钱 发布:2024-10-05 18:14:25 浏览:784
linux乱码问题 发布:2024-10-05 18:00:25 浏览:542
访客仪需要电脑做服务器吗 发布:2024-10-05 17:57:57 浏览:9
怎么在u盘设置密码 发布:2024-10-05 17:55:23 浏览:579
石器时代赚钱脚本 发布:2024-10-05 17:48:55 浏览:364
光存储器有哪些 发布:2024-10-05 17:48:20 浏览:706
java执行js 发布:2024-10-05 17:39:01 浏览:357