導航:首頁 > 版本升級 > struts2文件上傳例子

struts2文件上傳例子

發布時間:2024-12-28 09:33:58

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

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

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

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

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

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

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

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

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

B. 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>

C. 請問struts2能實現上百兆的視頻的上傳和下載嗎如果可以,怎麼配置文件類型

<!--上傳文件的大小限制-->
<constant name = "struts.multipart.maxSize" value = "10701096" />

在struts.xml中加如上面的代碼,其中的value值指的是文件的大小,以B為單位的.
<struts>
<constant name = "struts.multipart.maxSize" value = "10701096" />

<package name="test1" namespace="test1" extends="struts-default">
<action name="massage_*" class="org.zl.action.HelloWorldAction">
<result name="success">/WEB-INF/page/massage.jsp</result>
</action>
</package>

</struts>

D. commons-httpclient如何實現文件上傳

在struts2中結合HttpClient進行文件上傳
最近遇到了用httpclient進行上傳文件的問題,下面我就和大家簡單的說一下:
package com.imps.action;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class TabinfoAction extends BaseAction
{
private File[] myFile;
private String[] myFileFileName;// 文件名
private Integer[] myFileFileSize;// 文件大小
private String[] myFileContentType;// 文件類型

public String uploadPost()
{
String posturl ="http://127.0.0.1:8080/settabimage.aspx";
System.out.println(posturl);
String status=null;
for(int i=0;i<myFile.length;i++)
{
FileInputStream file=new FileInputStream(myFile[i]);
InputStream in = new BufferedInputStream(file);
PostMethod post=new PostMethod(posturl);
post.setRequestBody(in);
HttpClient client = new HttpClient();
client.executeMethod(post);
String response=new String(post.getResponseBodyAsString().getBytes("ISO-8859-1"),"UTF-8");
post.releaseConnection();
in.close();
file.close();
}
}

public File[] getMyFile() {
return myFile;
}

public void setMyFile(File[] myFile) {
this.myFile = myFile;
}

public String[] getMyFileFileName() {
return myFileFileName;
}

public void setMyFileFileName(String[] myFileFileName) {
this.myFileFileName = myFileFileName;
}

public Integer[] getMyFileFileSize() {
return myFileFileSize;
}

public void setMyFileFileSize(Integer[] myFileFileSize) {
this.myFileFileSize = myFileFileSize;
}

public String[] getMyFileContentType() {
return myFileContentType;
}

public void setMyFileContentType(String[] myFileContentType) {
this.myFileContentType = myFileContentType;
}
千萬記住不要記忘記關閉流和釋放http連接

E. struts2中的s:file標簽怎麼限定上傳文件為圖片類型,最好是能在選擇文件時就只能選擇圖片文件

<struts>
<package name="default" extends="struts-default">
<action name="upload" class="g2w.struts2.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
text/plain,text/css,text/javascript
</param>
<param name="maximumSize">1000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<param name="savePath">/uploads</param>
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
<result name="input">/failure.jsp</result>
</action>
</package>
</struts>

package g2w.struts2;

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 FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private File file;
private String fileContentType;
private String fileFileName;

private String savePath;

// setters & getters

public String execute() {
try {
File destFile = new File(this.getSavePath(), fileFileName);
FileUtils.File(this.file, destFile);
// ....
} catch (IOException e) {
this.setMessage(e.getMessage());
return ActionSupport.ERROR;
}
return SUCCESS;
}
}

F. struts2文件上傳中,如何限制上傳的文件類型

只需要在struts配置文件中配置就OK了
案例如下:

<package name="upload" extends="struts-default" namespace="/upload">
<!-- 配置 -->
<action name="upload" class="www.ijava.com.UploadAction" >

<param name="savePath">e:/images/</param>

<!--往fileuploadInterceptor 注入 -->
<interceptor-ref name="defaultStack">
<!-- 改變當前文件運行上傳的類型 -->
<param name="fileUpload.allowedTypes">image/jpeg,image/jpg</param>
<!-- 允許的文件後綴 -->
<param name="fileUpload.allowedExtensions">jpg,jpeg,gif</param>
</interceptor-ref>
<result>/index.jsp</result>
</action>

G. 關於struts2框架的文件上傳問題。。。上傳的文件超過2MB就報下面的異常,請問怎麼解決

在struts.xml中設置
<constant name="struts.multipart.maxSize" value="314572800"></constant> <!-- 允許300M -->
可以允許上傳300M的呢!我試了下,上傳了個202M的電版影,竟然上傳成功了!權

H. 我用struts2做的上傳文件功能,但當文件超過2G時頁面出現錯誤,這個有什麼辦法解決么

struts配置文件里有來個上傳大自小的常量可以配
可以配置default.properties文件,也可以直接配xml常量
常量名是struts.multipart.maxSize
值的話你自己換算就好了 1K*1024*1024什麼什麼的自己乘完放到value里

閱讀全文

與struts2文件上傳例子相關的資料

熱點內容
bat文件怎麼寫 瀏覽:117
http網路劫持怎麼解決 瀏覽:636
jsp中變數名參數啥意思 瀏覽:471
word工具隱藏 瀏覽:94
如何把電視網路弄好 瀏覽:739
ab5文件夾 瀏覽:505
數據模擬建模叫什麼 瀏覽:513
計算機網路可擴展性 瀏覽:809
加拿大28穩定qq群 瀏覽:263
網站設計思路怎麼寫 瀏覽:74
win10磁碟自檢文件丟失 瀏覽:475
win10掃描的文件在哪裡 瀏覽:615
pdf文件公章歪了怎麼處理 瀏覽:322
java下載文件的路徑 瀏覽:551
現在有哪些熱門的軟體編程 瀏覽:453
asp什麼文件迅雷下載 瀏覽:381
巫妖王之怒升級路線 瀏覽:348
wps如何發送文件 瀏覽:359
網站怎麼加流量 瀏覽:457
聖魔之光石破解版本 瀏覽:110

友情鏈接