struts2上傳配置
『壹』 如何配置struts2
這么簡單!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 設置字元集 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!--項目開發struts.devMode=true -->
<constant name="struts.devMode" value="true" />
<!-- 自動載入國際化資源文件,開發建議為ture -->
<constant name="struts.i18n.reload" value="true" />
<!--自動載入struts配置文件,開發建議為true -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 設置游覽器是否使用瀏覽器緩存,開發建議為false -->
<constant name="struts.serve.static.browserCache" value="false" />
<constant name="struts.ui.theme" value="simple" />
<!-- 添加struts配置文件 -->
<include file="config/struts/struts_book.xml" />
<include file="config/struts/struts_cart.xml" />
<include file="config/struts/struts_order.xml" />
<include file="config/struts/struts_user.xml" />
<package name="dang-default" extends="struts-default"
namespace="/base">
<!-- 公共struts信息 -->
<!-- 定義事務攔截器 -->
<interceptors>
<interceptor name="transactionInterceptor"
class="com.tarena.web.interceptor.TransactionInterceptor" />
<interceptor-stack name="dangStack">
<interceptor-ref name="transactionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 定義默認攔截器 -->
<default-interceptor-ref name="dangStack" />
<global-results>
<result name="login" type="redirect">/user/login_form.jsp</result>
</global-results>
</package>
</struts>
『貳』 struts2文件上傳有沒有最大限制
Struts2上傳文件的默認大小限制是2M(2097152位元組),也可以通過struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=2048 表示一次上傳文件的總大小不能超過2K位元組。
struts2上傳非常方便了,沒聽說網上有很多上傳插件,上傳核心代碼在後台,看你怎麼獲取上傳的文件數據怎麼將數據再寫入到本地伺服器。
『叄』 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文件上傳和下載
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>
這樣,就可以實現上傳和下載了.
『伍』 java中怎麼利用struts2上傳多個pdf文件
通過3種方式模擬多個文件上傳
第一種方式
package com.ljq.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
private File[] image; //上傳的文件
private String[] imageFileName; //文件名稱
private String[] imageContentType; //文件類型
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if (image != null) {
File savedir=new File(realpath);
if(!savedir.getParentFile().exists())
savedir.getParentFile().mkdirs();
for(int i=0;i<image.length;i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.File(image[i], savefile);
}
ActionContext.getContext().put("message", "文件上傳成功");
}
return "success";
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
}
第二種方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用數組上傳多個文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction2 extends ActionSupport{
private File[] image; //上傳的文件
private String[] imageFileName; //文件名稱
private String[] imageContentType; //文件類型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
//取得需要上傳的文件數組
File[] files = getImage();
if (files !=null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
//建立上傳文件的輸出流, getImageFileName()[i]
System.out.println(getSavePath() + "\\" + getImageFileName()[i]);
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()[i]);
//建立上傳文件的輸入流
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len=fis.read(buffer))>0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
}
return SUCCESS;
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上傳文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
第三種方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用List上傳多個文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction3 extends ActionSupport {
private List<File> image; // 上傳的文件
private List<String> imageFileName; // 文件名稱
private List<String> imageContentType; // 文件類型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
// 取得需要上傳的文件數組
List<File> files = getImage();
if (files != null && files.size() > 0) {
for (int i = 0; i < files.size(); i++) {
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName().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);
}
fis.close();
fos.close();
}
}
return SUCCESS;
}
public List<File> getImage() {
return image;
}
public void setImage(List<File> image) {
this.image = image;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
public List<String> getImageContentType() {
return imageContentType;
}
public void setImageContentType(List<String> imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上傳文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 該屬性指定需要Struts2處理的請求後綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts2處理。
如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。 -->
<constant name="struts.action.extension" value="do" />
<!-- 設置瀏覽器是否緩存靜態內容,默認值為true(生產環境下使用),開發階段最好關閉 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 當struts的配置文件修改後,系統是否自動重新載入該文件,默認值為false(生產環境下使用),開發階段最好打開 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 開發模式下使用,這樣可以列印出更詳細的錯誤信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默認的視圖主題 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解決亂碼 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="upload" namespace="/upload" extends="struts-default">
<action name="*_upload" class="com.ljq.action.UploadAction" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload1" namespace="/upload1" extends="struts-default">
<action name="upload1" class="com.ljq.action.UploadAction2" method="execute">
<!-- 要創建/image文件夾,否則會報找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload2" namespace="/upload2" extends="struts-default">
<action name="upload2" class="com.ljq.action.UploadAction3" method="execute">
<!-- 要創建/image文件夾,否則會報找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
上傳表單頁面upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上傳</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- ${pageContext.request.contextPath}/upload/execute_upload.do -->
<!-- ${pageContext.request.contextPath}/upload1/upload1.do -->
<!-- ${pageContext.request.contextPath}/upload2/upload2.do -->
<!-- -->
<form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="上傳" />
</form>
</body>
</html>
顯示頁面message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'message.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
上傳成功
<br/>
<s:debug></s:debug>
</body>
</html>
『陸』 使用struts2 怎麼實現 文件上傳到另外一台電腦
傳輸文件的功能,類似於網路聊天程序。
肯定要用的文件傳輸用的IO流,還有網路通信用到的socket了。
可以在你部署struts2網站的伺服器上面寫一個網路通信程序(伺服器端),對應的網路通信程序(客戶端)放在「另外一台電腦」上面。
網站的伺服器啟動之後:
1。把那個網路通信程序(伺服器端)啟動
2。把「另外一台電腦」上面的網路通信程序(客戶端)啟動,現在兩端就建立連接了。
3。可以通過伺服器端向客戶端發送數據。
以上過程跟我們平時用的聊天程序一樣。
你可以在網上看看相應的網路聊天程序,現在網上這樣的程序還是很多的。
裡面實現了這樣的機制。