为什么struts2上传文件自动保存到tomcat下,如何更改成自定义路径?
Stringroot=ServletActionContext.getRequest().getRealPath("D:\\新建文件夹");;我认为改这句代码,可是系统提...
String root=ServletActionContext.getRequest().getRealPath("D:\\新建文件夹");
;我认为改这句代码,可是系统提示我找不到文件夹(系统自动去tomcat文件夹下面找了) 展开
;我认为改这句代码,可是系统提示我找不到文件夹(系统自动去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" ;
}
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" ;
}
展开全部
服务器的根目录是tomcat的webapps,c盘、d盘都是本地的。相誉如当于你电脑同时模拟客户端和服灶巧务器两台电脑,web程序在服务器这台电脑上,d盘在客户端上,当然找到不d盘。如果要测试,你可以直接用java最基本隐虚键的io方法写本地程序,new File(String path);之类的。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ServletActionContext 这个context的上下文环衡做境就是你的webapp,而你的扮拦毕app是在tomcat下
所以肯定是从tomcat下面找,如果厅芹你愿意 你可以把路径设置为任意你想要的
所以肯定是从tomcat下面找,如果厅芹你愿意 你可以把路径设置为任意你想要的
更多追问追答
追问
那需要怎么修改自动路径?
追答
貌似保存那块代码还是要你自己写的 路径你高兴怎么配置就怎么配置就是了 什么叫自动路径?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写完整路径就是了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询