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

struts2上傳下載

發布時間: 2023-01-14 10:49:50

『壹』 1、struts2 如何上傳文件並顯示在jsp頁面上,提供下載連接 2、struts2 如何下載顯示在jsp頁面上的文件

http://hi..com/kawnj19890209/blog/item/8c64d78e139c88f4f11f36fa.html可以看看這兒

『貳』 struts2中,文件上傳和下載時候的緩沖區大小多少合適如題 謝謝了

文件通常默認是15MB的允許最大上傳的文件,緩沖其實設為4096K就好了。和文件大小沒關系。只跟網速度有關,設大了也沒用。

『叄』 struts2上傳下載

07和03版的EXCEL用的是不同的擴展名,如果你用POI之類的jar包處理office文檔的話,處理這兩種格式文件的方法也是不一樣的

『肆』 struts2 文件的上傳和下載,怎麼控制上傳的文件類型啊

<interceptor-ref name="fileUpload">
<param name="allowedTypes">application/octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/msexcel</param>
<param name="maximumSize">10048576</param>
</interceptor-ref>
具體內容去struts教程網看看吧。

『伍』 struts2實現圖片的上傳和下載

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import com.zdvictory.taurus.common.util.*;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;

/** *
*/
public class UploadFileHandler {

private static int BUFFER_SIZE = 8192;

/**
* 上傳附件操作 傳遞參數:系統參數配置設置的參數名稱
*/
@SuppressWarnings("unchecked")
public static List<Attachment> upload(String sysParaName) throws Exception {
// 文件保存路徑
String path = SysParaFinder.getSysParaValue(sysParaName);
List<Attachment> list = new ArrayList<Attachment>();
MultiPartRequestWrapper request = (MultiPartRequestWrapper) ServletActionContext
.getRequest();
Enumeration enu = request.getFileParameterNames();
while (enu.hasMoreElements()) { // 對每一個文件域進行遍歷
String controlName = (String) enu.nextElement();
String[] fileNames = request.getFileNames(controlName);
File[] uploadFiles = request.getFiles(controlName);
for (int i = 0; i < uploadFiles.length; i++) {
File uploadFile = uploadFiles[i];
if (!uploadFile.exists())
continue;
// 如果文件夾不存在,創建文件夾,將文件保存到目錄
File dir = new File(request.getRealPath("/") + path);
if (!dir.exists())
dir.mkdirs();
String ext = fileNames[i].substring(fileNames[i].indexOf("."),
fileNames[i].length());// 獲取文件擴展名
String filename = UUID.randomUUID().toString() + ext;
File file = new File(request.getRealPath("/") + path + filename);
byte[] data = new byte[BUFFER_SIZE];
int byteRead = -1;
FileInputStream in = new FileInputStream(uploadFile);
FileOutputStream out = new FileOutputStream(file);
while ((byteRead = in.read(data)) != -1) {
out.write(data, 0, byteRead);
out.flush();
}
out.close();
in.close();
// 設置附件對象屬性
Attachment attach = new Attachment();
attach.setFilename(fileNames[i]);
attach.setContentType(ext);
attach.setFilepathname(path + filename);
attach.setFilesize(uploadFile.length());
list.add(attach);
}
}
return list;
}
}
文件下載

public String download() throws Exception {
redheadTemplate = redheadTemplateManager.findById(Long
.valueOf(getId()[0]));
String name = redheadTemplate.getName()
+ redheadTemplate.getFilepathname().substring(
redheadTemplate.getFilepathname().lastIndexOf("."),
redheadTemplate.getFilepathname().length());
this.setFilename(new String(name.getBytes(), "ISO8859-1"));
this.setFilepathname(redheadTemplate.getFilepathname());
return "download";
}
文件下載配置文件

<result name="download" type="stream">
<!-- 下載文件類型 -->
<param name="contentType">
application/octet-stream
</param>
<!-- 默認為 inline(在線打開),設置為 attachment 將會告訴瀏覽器下載該文件,filename 指定下載文
件保存時的文件名,若未指定將會是以瀏覽的頁面名作為文件名,如以 download.action 作為文件名,
這里使用的是動態文件名,${filename}, 它將通過 Action 的 getFilename() 獲得文件名 -->
<param name="contentDisposition">
attachment;filename="${filename}"
</param>
<!-- 下載的InputStream流,Struts2自動對應Action中的getDownloadFile方法,該方法必須返回InputStream類型 -->
<param name="inputName">downloadFile</param>
<!-- 輸出時緩沖區的大小 -->
<param name="bufferSize">8192</param>
</result>

『陸』 struts2文件上傳和下載

上傳:

package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
publicclass UploadAction2 extends ActionSupport {

// 封裝上傳文件域的屬性
private File image;
// 封裝上傳文件類型的屬性
private String imageContentType;
// 封裝上傳文件名的屬性
private String imageFileName;
// 接受依賴注入的屬性
private String savePath;

@Override
public String execute() {
FileOutputStream fos =null;
FileInputStream fis =null;
try {
// 建立文件輸出流
System.out.println(getSavePath());
fos =new FileOutputStream(getSavePath() +"\\"+ getImageFileName());
// 建立文件上傳流
fis =new FileInputStream(getImage());
byte[] buffer =newbyte[1024];
int len =0;
while ((len = fis.read(buffer)) >0) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
System.out.println("文件上傳失敗");
e.printStackTrace();
} finally {
close(fos, fis);
}
return SUCCESS;
}

/**
* 返回上傳文件的保存位置
*
* @return
*/
public String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath);
}

publicvoid setSavePath(String savePath) {
this.savePath = savePath;
}

public File getImage() {
return image;
}

publicvoid setImage(File image) {
this.image = image;
}

public String getImageContentType() {
return imageContentType;
}

publicvoid setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}

public String getImageFileName() {
return imageFileName;
}

publicvoid setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

privatevoid close(FileOutputStream fos, FileInputStream fis) {
if (fis !=null) {
try {
fis.close();
} catch (IOException e) {
System.out.println("FileInputStream關閉失敗");
e.printStackTrace();
}
}
if (fos !=null) {
try {
fos.close();
} catch (IOException e) {
System.out.println("FileOutputStream關閉失敗");
e.printStackTrace();
}
}
}
}

『柒』 Struts.2的文件上傳下載為什麼要繼承ActionSupport類

不繼承這個類也同樣可以實現的。但是習慣上繼承這個類。

熱點內容
cpu訪問存儲器時的特性 發布:2025-09-05 09:17:13 瀏覽:982
博途軟體已經編譯但是程序有錯 發布:2025-09-05 09:06:10 瀏覽:481
蘋果手機密碼鎖了怎麼解鎖 發布:2025-09-05 09:00:30 瀏覽:886
apache埠修改linux 發布:2025-09-05 08:42:22 瀏覽:718
kafka源碼編譯 發布:2025-09-05 08:30:21 瀏覽:586
用電腦怎麼看無線密碼 發布:2025-09-05 08:17:54 瀏覽:955
aspmvc源碼 發布:2025-09-05 08:11:18 瀏覽:398
手機解鎖密碼忘了怎麼取消手機解鎖密碼 發布:2025-09-05 08:01:51 瀏覽:319
花臉安卓怎麼下載 發布:2025-09-05 07:50:02 瀏覽:997
python一行寫for 發布:2025-09-05 07:49:31 瀏覽:500