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

struts1上傳文件

發布時間: 2023-06-06 01:54:33

Ⅰ struts上傳多張圖片怎麼做,要求可以說上傳中文的,而且圖片可以重復上傳不被覆蓋

你指的是一次上傳多張嗎?我這有個自己寫的例子:
//上傳實體類
public class Upload {
private File photoes; //封裝上傳文件的屬性
private String photoesFileName; //封裝上傳文件的名稱屬性
private String photoesContentType; //封裝上傳文件的類型屬性
private String targetdir; //保存路徑
private String targetfilename; //保存的文件名

public File getPhotoes() {
return photoes;
}
public void setPhotoes(File photoes) {
this.photoes = photoes;
}
public String getPhotoesFileName() {
return photoesFileName;
}
public void setPhotoesFileName(String photoesFileName) {
this.photoesFileName = photoesFileName;
}
public String getPhotoesContentType() {
return photoesContentType;
}
public void setPhotoesContentType(String photoesContentType) {
this.photoesContentType = photoesContentType;
}
public String getTargetdir() {
return targetdir;
}
public void setTargetdir(String targetdir) {
this.targetdir = targetdir;
}
public String getTargetfilename() {
return targetfilename;
}
public void setTargetfilename(String targetfilename) {
this.targetfilename = targetfilename;
}
}
這是上傳圖片的Action類中的部分代碼
//獲得伺服器上保存上傳文件的目錄images的絕對路徑
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
//設置保存文件的目錄
uld.setTargetdir(realpath);
//設置目標文件名
uld.setTargetfilename(generateFileName(uld.getPhotoesFileName()));
//把doc內容復制到target
FileUtils.File(uld.getPhotoes(), new File(uld.getTargetdir(),uld.getTargetfilename()));

Ⅱ ssh 多文件上傳,怎麼實現

多附件struts 1.x,以下代碼由agatezone提供。
1.Jsp要用javascript
2.form必須enctype="multipart/form-data"
3.action要用form.getMultipartRequestHandler()獲取文件並存儲
struts2 更簡單,google之。

<%@ 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>struts1.x upload example</title>
<script type="text/javascript">
function addFileField() {
input_filesCount = document.getElementById("filesCount");
input_filesCount.setAttribute("value", parseInt(input_filesCount.getAttribute("value")) + 1)
div_files = document.getElementById("files");
br = document.createElement("br");
file = document.createElement("input");
text = document.createTextNode("File " + input_filesCount.getAttribute("value") + " : ");
file.setAttribute("type", "file");
file.setAttribute("name", "file" + input_filesCount.getAttribute("value"));
div_files.appendChild(text);
div_files.appendChild(file);
div_files.appendChild(br);
}
</script>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
<input id="filesCount" type="hidden" name="filesCount" value="1" />
<div id="files">
File 1 :
<input type="file" name="file1" />
<br />
</div>
<input type="button" onclick="javascript:addFileField();"
value="add a file" />
<br />
<input type="submit" />
</form>
</body>
</html>

---------------------------------------
package cn.agatezone.example.struts1x.upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class UploadAction extends Action {
@SuppressWarnings("unchecked")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {

String dirPath = getServlet().getServletContext().getRealPath("/") + "upload";
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
for (Enumeration e = fileh.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
try {
FormFile formfile = (FormFile) fileh.get(key);
String filename = formfile.getFileName().trim(); // 文件名
/*
* @注意!!!
* 這里我沒有處理中文,但是如果想要中文無問題,
* 可以設置tomcat的server.xml中的URIEncoding="UTF-8"
*
* 但是,要是不設置的話可以自己用代碼解決問題!
* 為了簡單明了,本例只作為struts1.x上傳部分展示。
*/
if (!"".equals(filename)) {
// 不同的瀏覽器傳上的文件名可能有區別,有的是全路徑的
// 在這里保存文件
InputStream ins = formfile.getInputStream();
OutputStream os = new FileOutputStream(dirPath + File.separatorChar + filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();

}
} catch (Exception ex) {
System.out.println("出錯了!\n" + ex);
}
}

return mapping.findForward("success");

}

}

---------------------------------------------------------
銳志陳鵬 專注Java/.Net培訓
銳志技術社區:http://www.witshare.org/bbs/

Ⅲ strut上傳文件報錯java.lang.OutOfMemoryError: Java heap space

超出虛擬機內存了吧,改下jvm內存,網上有很多設置的帖子的

熱點內容
壓縮機過熱保護器在哪 發布:2025-02-07 13:03:21 瀏覽:42
安裝win8需要什麼配置 發布:2025-02-07 13:00:34 瀏覽:676
大板演算法 發布:2025-02-07 12:56:47 瀏覽:254
tplink路由器如何配置 發布:2025-02-07 12:50:48 瀏覽:428
unicode轉中文python 發布:2025-02-07 12:45:21 瀏覽:287
學習python用什麼軟體 發布:2025-02-07 12:45:15 瀏覽:611
怎麼看bin文件編譯日期 發布:2025-02-07 12:44:27 瀏覽:390
怎麼啟動ftp服務 發布:2025-02-07 12:27:46 瀏覽:865
拜託別黑我ftp 發布:2025-02-07 12:25:22 瀏覽:170
評價web伺服器的標準是什麼 發布:2025-02-07 12:24:37 瀏覽:444