當前位置:首頁 » 文件管理 » 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 20:20:47 瀏覽:886
移動寬頻設置路由器怎麼設置密碼 發布:2024-10-05 20:03:30 瀏覽:105
微指令的編譯方法有哪一些 發布:2024-10-05 19:02:10 瀏覽:885
android離線定位 發布:2024-10-05 18:36:40 瀏覽:858
ipad4密碼忘記怎麼辦 發布:2024-10-05 18:36:07 瀏覽:237
黑莓加密天線 發布:2024-10-05 18:30:07 瀏覽:851
編程入行年齡 發布:2024-10-05 18:29:24 瀏覽:540
伺服器地址訪問不到 發布:2024-10-05 18:20:55 瀏覽:691
手機解鎖忘記密碼多少錢 發布:2024-10-05 18:14:25 瀏覽:787
linux亂碼問題 發布:2024-10-05 18:00:25 瀏覽:545