例如,每个上传文件有一个file,有多个。
file的名字是它名字加1,
例如c:\\小白1,d:\\小白2,
例如有5个。我想在action中做接受做个请求怎么做?希望能附上已经成功的代码。
不知道为什么我就是拿不到它的名字?

解决方案 »

  1.   

    package action;import java.io.File;
    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;
    }}
      

  2.   

    用什么做上传?
    struts2上传得到文件名很简单,前面jsp <input type="file" name="file">  
    <form >标签里填上  enctype="multipart/form-data"action类里定义private String fileName;   //得是前面定义的name属性的值+Name
    加上get/set方法。fileName就是你上传的文件名了,同理fileContentType 就是你上传文件的类型了。
      

  3.   


    写错了点。 应该是name属性值+FileName.  即fileFileName…
      

  4.   

    用struts2的话,假如上传多个文件的话,最常规的写法:
    private List listFile;
    private List listFilaName;
    ......
      

  5.   

     //IO流
        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);
             }
      

  6.   

    1.Action file必须和表单元素名称对应  在你的<input name="file" type="file"/>
    2.      private File file;// 附件
    private String fileContentType; // 得到文件的类型
            private String fileFileName; // 得到上传文件名称
          这三个属性需要有set和get方法
    3. enctype multipart/form-data
      

  7.   

    你要明白上传的附件的名字不是无限增加的
    例如file1,file2,file3,它不是固定的。
      

  8.   

    你要明白上传的附件的名字是无限+1组成增加的
    例如file1,file2,file3,它不是固定的。
      

  9.   

    Struts2 做上传 如果是多个文件,可以用集合或数组接收
      

  10.   

    我也知道是用集合和数组啊。主要是它的名字怎么取啊。它们file的名字都不一样。不知道咋取