例如,每个上传文件有一个file,有多个。
file的名字是它名字加1,
例如c:\\小白1,d:\\小白2,
例如有5个。我想在action中做接受做个请求怎么做?希望能附上已经成功的代码。
不知道为什么我就是拿不到它的名字?
file的名字是它名字加1,
例如c:\\小白1,d:\\小白2,
例如有5个。我想在action中做接受做个请求怎么做?希望能附上已经成功的代码。
不知道为什么我就是拿不到它的名字?
解决方案 »
- Struts2的<s:url action="XXX" namespace="/XXX"/>的Bug问题
- SSH如何启动服务时检测数据库是否存在!!!!!
- 求个VPN号,能用分全给了
- Web Service 高手请进
- 问下,我要搭个Spring MVC 的框架,返回视图模板是JSTL,需要添加哪些JAR包
- 求javax.media.jai包
- 请问那里有jbuilder开发spring的插件下载
- tomcat鎖定jsp頁面問題
- 有谁看过“Struts在行动”这本书?
- Struts环境配置手顺(eclipse版),包括SQL Server的连接!
- JAVA EE 基于JBOSS服务的WEB应用实例
- JDBC:DriverManager.registerDriver(new com.mysql.jdbc.Driver()););
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {
private File upload; // 保存上传的文件
private String uploadContentType; // 保存上传的文件类型
private String uploadFileName; // 保存上传的文件名 public String execute() throws Exception {
// System.out.println("谁动了我的upload " + photoId);
String address = ServletActionContext.getServletContext().getRealPath(
"/");
String serverName = ServletActionContext.getRequest().getScheme()
+ "://" + ServletActionContext.getRequest().getServerName()
+ ":" + ServletActionContext.getRequest().getServerPort()
+ ServletActionContext.getRequest().getContextPath() + "/";
List<Map<String, Object>> photoAddress = this.uploadPicture(serverName,
address, 123, upload);
return SUCCESS;
} public List<Map<String, Object>> uploadPicture(String serverName,
String address, int aaa, File upload) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
// Student stu=studentInfoDao.getStudentUniqueById(id);
String imageName = aaa + ".jpg";
address = address + "images/student/" + imageName;
FileOutputStream fos = null;
InputStream is = null;
try {
fos = new FileOutputStream(address);
is = new FileInputStream(upload);
System.out.println("file length is " + upload.length());
byte[] buffer = new byte[(int) upload.length()];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
// -----------------调用保存图片-----------------------
// this.savePhoto(id, buffer);
fos.close();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, Object> mh = new HashMap<String, Object>();
//mh.put("id", id);
mh.put("name", serverName + "images/student/" + imageName);
list.add(mh);
return list;
} // public File getUpload() {
// return upload;
// } public void setUpload(File upload) {
this.upload = upload;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}}
struts2上传得到文件名很简单,前面jsp <input type="file" name="file">
<form >标签里填上 enctype="multipart/form-data"action类里定义private String fileName; //得是前面定义的name属性的值+Name
加上get/set方法。fileName就是你上传的文件名了,同理fileContentType 就是你上传文件的类型了。
写错了点。 应该是name属性值+FileName. 即fileFileName…
private List listFile;
private List listFilaName;
......
private File file;
private String fileFileName;
private String fileContentType;这些免掉了,你知道的。set get页面的form就加上enctype="multipart/form-data"if(file!=null){
if(!fileContentType.equals("image/pjpeg") && !fileContentType.equals("image/gif") && !fileContentType.equals("image/jpeg") ){
addFieldError("ticket.image", "图片格式有误!");
return "addNew";//跳回增加工单页面
}
//图片大小不能大于3072kb(3MB)
if(file.length()/1024 > 3072 ){
addFieldError("ticket.image", "图片不能大于3MB!");
return "addNew";//跳回增加工单页面
}
String url = "images\ticket\upload";
String path=ServletActionContext.getServletContext().getRealPath(url);
File f=new File(new File(path),time+"-"+fileFileName);
if(!f.getParentFile().exists()){
f.getParentFile().mkdirs();
}
FileUtils.copyFile(file,f);
ticket.setImage(url+"\"+time+"-"+fileFileName);
}
2. private File file;// 附件
private String fileContentType; // 得到文件的类型
private String fileFileName; // 得到上传文件名称
这三个属性需要有set和get方法
3. enctype multipart/form-data
例如file1,file2,file3,它不是固定的。
例如file1,file2,file3,它不是固定的。