struts中的上传问题

         很多人在用struts做上传的时候都会遇到一相同的问题,那就是信息里出现Removing ........然后在文件中找不到你选择上传的东西。其实要看后台的action代码。主要的文件保存,以及文件的路径。还有Struts中配置的uploadDir。

后台action的主要代码如下:

package cdu.liuy.action; 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import cdu.liuy.entity.Image;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadForm extends ActionSupport{   
 
private File file;//上传文件的file对象
 private String fileFileName;//上传文件的名称
 private String fileContentType;//上传文件的MIME类型
 private String description;//上传的描述信息
 private String uploadDir;//保存上传文件的目录
 public File getFile() {
  return file;
 }
 public void setFile(File file) {
  this.file = file;
 }
 public String getFileFileName() {
  return fileFileName;
 }
 public void setFileFileName(String fileFileName) {
  this.fileFileName = fileFileName;
 }
 public String getFileContentType() {
  return fileContentType;
 }
 public void setFileContentType(String fileContentType) {
  this.fileContentType = fileContentType;
 }
 public String getDescription() {
  return description;
 }
 public void setDescription(String description) {
  this.description = description;
 }
 public String getUploadDir() {
  return uploadDir;
 }
 public void setUploadDir(String uploadDir) {
  this.uploadDir = uploadDir;
 }  
  public String execute() throws Exception {
         //根据服务器的文件保存地址和原文件名创建目录文件全路径
         String dir =  ServletActionContext.getServletContext().getRealPath(uploadDir)
         +"//" +"news"+this.fileFileName; //文件将保存在这个目录下。如果不知道目录的位置。你可以在这里加一句输出语句System.out.println("dir路径为:"+dir);//在控制台输出dir路径.上传成功后直接在这个目录路径下去找上传的图片。
       
         System.out.println("上传的文件的类型:"+this.fileContentType);
        System.out.println("文件地址"+dir);
         File di = new File(dir);
         copy(this.file, di);
         return SUCCESS;
     }
  private static void copy(File src, File dst) {
         InputStream in = null;
         OutputStream out = null;
         try {
             in = new BufferedInputStream(new FileInputStream(src), 2048);
             out = new BufferedOutputStream(new FileOutputStream(dst),2048);
             byte[] buffer = new byte[2048];
             int len = 0;
             while ((len = in.read(buffer)) > 0) {
                 out.write(buffer, 0, len);

             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if (null != in) {
                 try {
                     in.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             if (null != out) {
                 try {
                     out.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     } 
}  

 

你可能感兴趣的:(struts,string,file,exception,buffer,null)