struts框架-实现文件上传

总体思路:struts框架内部实现了将文件上传的功能

                 例如:

                        1、jsp页面form表单: 


                                                              

                                                               
                                                      

                        (注意:一定要写enctype="multipart/form-data",不然默认文件无法上传)

                        2、.xml配置文件,这个和寻常的一样,就不写了

                        3、action文件:private File uploadFile;
                                                  private String uploadFileFileName;
                                                  public File getUploadFile() {
                                                           return uploadFile;
                                                   }
                                                  public void setUploadFile(File uploadFile) {
                                                           this.uploadFile = uploadFile;
                                                    }
                                                  public String getUploadFileFileName() {
                                                           return uploadFileFileName;
                                                    }
                                                  public void setUploadFileFileName(String uploadFileFileName) {
                                                                 this.uploadFileFileName = uploadFileFileName;
                                                    }

                                 在方法体中写System.out.println(uploadFile),即是struts框架将我们上传的文件放到的临时地方;uploadFileFileName是我们上传文件的文件名

 

接下来,我们要做的工作实则是将struts帮我们实现上传的那个文件拷贝到我们指定的文件夹中即可。

                实现代码(新建一个UploadFileCopyUtil类):

                                 package com.onlineproject.util;

                                 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.io.OutputStream;

                                 public class UploadFileCopyUtil {

                                public static String copy(File sourceFile, String destDirPath,
                                               String destFileName) throws IOException {

                                          InputStream inputStream = null;
                                          OutputStream outputStream = null;
                                   try {
                                          inputStream = new FileInputStream(sourceFile);

                                          File distDir = new File(destDirPath);
                                          if (!distDir.exists()) {
                                                   distDir.mkdirs();
                                            }
                                         File destFile = new File(distDir.getAbsolutePath() + "/"
                                                                               + destFileName);

                                         if (destFile.exists()) {
                                                      destFileName = System.currentTimeMillis() + "_" + destFileName;
                                                      destFile = new File(distDir.getAbsolutePath() + "/"
                                                                                           + destFileName);
                                          }

                                         // 创建输出流
                                          outputStream = new FileOutputStream(destFile);

                                         // 读出输入流的数据,并且放到输出流
                                           byte datas[] = new byte[1024];

                                          while (true) {
                                                   int len = inputStream.read(datas);
                                                    if (len == -1) {
                                                                        break;
                                                      }
                                                  byte destDatas[] = new byte[len];
                                                  if (len < 1024) {
                                                           System.arraycopy(datas, 0, destDatas, 0, len);
                                                           outputStream.write(destDatas);

                                                    } else {
                                                            outputStream.write(datas);
                                                     }

                                            }

                                    } catch (FileNotFoundException e) {
                                              e.printStackTrace();
                                    } finally {

                                     try {
                                             inputStream.close();
                                             outputStream.close();
                                          } catch (IOException e) {
                                                     e.printStackTrace();
                                             }
                                         }

                                            return destFileName;
                                    }
                           }

调用代码(action方法体中):public String upload(){
                                                 File file = new File(TestUploadAction.class.getResource("/").getFile());
                                                 System.out.println(file.getParentFile().getParentFile().getAbsolutePath()+"/upload");
                                                 String saveStr;
                                                  try {
                                                        saveStr = UploadFileCopyUtil.copy(uploadFile, file.getParentFile().getParentFile().getAbsolutePath()+"/upload", uploadFileFileName);
                                                  } catch (IOException e) {
                                                         e.printStackTrace();
                                                  }
                                                         return "success";
                                                   }

 

 

你可能感兴趣的:(java(hibernate,struts))