当前位置:首页 » 文件管理 » 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 09:49:15 浏览:53
享宇钱包上传不了照片 发布:2025-02-07 09:48:32 浏览:632
模拟器怎么开安卓版本 发布:2025-02-07 09:42:35 浏览:771
c程序设计语言源码 发布:2025-02-07 09:22:41 浏览:623
数据库域的概念 发布:2025-02-07 09:22:40 浏览:640
电信路由器账号密码多少 发布:2025-02-07 09:22:01 浏览:671
我的世界怎么在别人服务器开光影 发布:2025-02-07 09:21:20 浏览:12
qq上传视频很慢怎么办 发布:2025-02-07 09:16:04 浏览:697
pythonredis缓存 发布:2025-02-07 09:10:24 浏览:928
封边机主要看哪些配置 发布:2025-02-07 09:10:17 浏览:906