當前位置:首頁 » 文件管理 » struts2上傳文件

struts2上傳文件

發布時間: 2022-01-24 22:15:53

⑴ struts2怎麼接收上傳文件

新建Web Project,在WebRoot下新建upload文件夾
在WebRoot下新建upload.jsp,上傳界面

編寫上傳成功、失敗的提示界面。
在WebRoot下新建uploadError.jsp

在WebRoot下新建 uploadSuccess.jsp

編寫Action類

配置struts.xml文件,重置fileUpload攔截器。

7
測試,測試完成之後在tomcat下面webapps目錄下找到項目對應的文件夾下的upload下查看

⑵ struts2 如何通過ajax上傳文件

ajax是不能上傳文件的,一般做法是使用一個隱藏的iframe 來個傳,達到無刷新上傳的效果。
還有就是使用swf上傳控制項,swfUpload等

⑶ 使用struts2 怎麼實現 文件上傳到另外一台電腦

傳輸文件的功能,類似於網路聊天程序。

肯定要用的文件傳輸用的IO流,還有網路通信用到的socket了。

可以在你部署struts2網站的伺服器上面寫一個網路通信程序(伺服器端),對應的網路通信程序(客戶端)放在「另外一台電腦」上面。

網站的伺服器啟動之後:
1。把那個網路通信程序(伺服器端)啟動

2。把「另外一台電腦」上面的網路通信程序(客戶端)啟動,現在兩端就建立連接了。

3。可以通過伺服器端向客戶端發送數據。

以上過程跟我們平時用的聊天程序一樣。

你可以在網上看看相應的網路聊天程序,現在網上這樣的程序還是很多的。
裡面實現了這樣的機制。

⑷ struts2文件上傳和下載

1,上傳方法
(1),頁面form表單添加一個屬性為enctype="multipart/form-data" 和method="post"

(2),假設上傳預覽框為 <input type="file" name="myfile" />
(3),控制器接值的方法為
private File myfile; //要上傳的文件

private String myfileFileName; //要上傳文件名稱
private String myfileContentType; //要上傳文件類型
別忘了做set方法

(4), 接到值後可以保存到資料庫,也可以保存到硬碟,
>>1 保存到資料庫, 資料庫表中對應欄位要設置為BLOB類型

>>2 保存到硬碟代碼如下

InputStream in = new
FileInputStream( myfile);
OutputStream out = new
FileOutputStream( new File("d:\\upload\\"+myfileFileName));
byte[] buffer
= new byte[ in.available() ];
int ins =
in.read(buffer);//讀取位元組到buffer中
//ins == -1 時
。就已經是文件的結尾了
while ( ins !=
-1 ) {
out.write(buffer, 0, ins);//將緩存buffer中的數據寫到文件中
ins = in.read(buffer);
}

in.close();
out.flush();
out.close();
2,下載
(1), 把要下載的文件轉成一個輸入流InputStream
例如,利用hibernate取得一個文件,文件類型在實體類中為byte[]類型,

inputStream = new
ByteArrayInputStream(book.getMyfile);
其中inputStream 為全局變數,並且做setter和getter方法
(2),在控制器對應的action節點中(struts2配置文件中)添加一個result節點如下:

<result name="download" type="stream">
<param name="contentType">application/zip</param>

<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${myFileFileName}"</param>
<param name="bufferSize">1024</param>
</result>
這樣,就可以實現上傳和下載了.

⑸ struts2 如何實現上傳整個文件夾的功能

一、壓縮文件其實是可以0壓縮率直接打包,這樣其實蠻快的
二、看到網上說Applet可以上傳文件夾,具體遠離不清楚,你可以看看
三、最笨的方法,用Ajax做一個遞歸遍歷文件夾的函數,如果是文件就上傳上去,如果是文件夾就請求後台新建文件夾
四、用JSON格式把目錄和文件封裝起來,統一傳到後台,但是後台處理要比較麻煩

⑹ 在struts2的action中如何獲得上傳文件客戶端的路徑


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport
{
public InputStream getDownloadFile()

{
InputStream is = null;
try {
is = new FileInputStream("f:/java.rar");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return is;
}

@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
現寫了一個 看的懂吧 祝你成功!

⑺ 如何實現struts2的文件上傳

(upload.jsp)
<body>
<s:form action="loadPage" namespace="/load" enctype="multipart/form-data">
<s:file name="file" label="select a file"></s:file>
<s:textfield name="newFileName" label="文件新名字"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
<s:actionerror/>
</body>

(struts-upload.xml)

<package name="load" extends="struts-default" namespace="/load">
<global-results>
<result name="input">/WEB-INF/pages/load/load.jsp</result>
</global-results>

<action name="load" class="com.tj.beef.action.LoadPageAction" method="input">
</action>

<action name="loadPage" class="com.tj.beef.action.LoadPageAction">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">102400</param>
<param name="fileUpload.allowedTypes">image/gif,image/pjpeg</param>
</interceptor-ref>
<param name="loadDir">/img/</param>
<result>/WEB-INF/pages/load/success.jsp</result>

</action>
</package>

(LoadPageAction)

package com.tj.beef.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoadPageAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContentType;
private String loadDir;
private String newFileName;

public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}

public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getLoadDir() {
return loadDir;
}
public void setLoadDir(String loadDir) {
this.loadDir = loadDir;
}
public String getNewFileName() {
return newFileName;
}
public void setNewFileName(String newFileName) {
this.newFileName = newFileName;
}
@Override
public String execute() throws Exception {
System.out.println(file);
System.out.println(fileFileName);
System.out.println(fileContentType);
System.out.println(loadDir);
System.out.println(newFileName);
//get path
String path = ServletActionContext.getServletContext().getRealPath(this.loadDir);
//hava file?
File dir = new File(path);
if(!dir.exists()) {
dir.mkdir();
}
//
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);

File newFile = new File(path,fileFileName);
FileOutputStream fos = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//
byte[] buf = new byte[4096];
int len = bis.read(buf);
while(len!=-1) {
bos.write(buf,0,len);
len = bis.read(buf);
}
bis.close();
fis.close();
bos.close();
fos.close();

return SUCCESS;
}
@Override
public String input() throws Exception {
// TODO Auto-generated method stub
return INPUT;
}

}

⑻ struts2上傳文件

File saveFile=new File(saveDir, imageFileName);不會創建一個空文件,這只是為了把savaFile做為一個參數傳到FileUtils.File(image, saveFile);里用來讀寫文件。
第二個是為什麼不直接File saveFile=new File(realpath,imageFileName);因為需要先創建文件夾然後在里邊寫文件才這么做的

⑼ struts2文件上傳

引入commons-fileupload 的jar包,頁面標簽是file類型。後台直接按前台標簽的名字按流讀取就行了。跟普通的上傳下載沒區別。

熱點內容
license伺服器是什麼 發布:2024-10-18 20:13:56 瀏覽:975
我的世界電腦幾種最好玩的伺服器 發布:2024-10-18 20:10:22 瀏覽:399
手機聯系人雲存儲 發布:2024-10-18 19:56:04 瀏覽:529
dnd編程 發布:2024-10-18 19:37:58 瀏覽:110
matlab編程學習 發布:2024-10-18 19:12:53 瀏覽:456
c語言賦值函數 發布:2024-10-18 19:10:43 瀏覽:967
ftp3級 發布:2024-10-18 18:57:11 瀏覽:45
python的zip 發布:2024-10-18 18:56:05 瀏覽:574
sql2008清理日誌 發布:2024-10-18 18:38:37 瀏覽:462
linux實戰項目 發布:2024-10-18 18:30:20 瀏覽:359