上传文件 JSP+java

来源:互联网 发布:vscode c 生成exe 编辑:程序博客网 时间:2024/05/02 02:25
上传文件
1。JSP
  form 中添加 enctype="multipart/form-data"
   附件<input type="file"  size="50" name="UploadFile" value='<c:out value="${UploadFile}"/>' />

 

2。java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

   preCheck(); //鉴定
   FileProperties salarypro =
     new FileProperties("/HostRoute.properties");
   String outFilepath = salarypro.getProperty("FBSFtpLocalPath").trim();
   //outFilepath = "d:/";
   String outFilename = outFilepath  + "0" + seqNo + ".rar";
         File upFile = getUpLoadFile("UploadFile");
 
   byte fileByte[] = new byte[Integer.parseInt(Long.toString(upFile.length()))];
   try {
 
    FileInputStream ddd = new FileInputStream(upFile);
    ddd.read(fileByte);
    ddd.close();
    FileOutputStream outf = new FileOutputStream(new File(outFilename));
    outf.write(fileByte);
    outf.close();

   } catch ( IOException e) {
     System.err.println( "upload file error! " + e.toString() );
     throw new CsiiException("上传文件失败");
 
    }catch( Exception e3 ) {
      System.err.println( "upload file error! " + e3.toString() );
     throw new CsiiException("上传文件失败");
   }

public void preCheck() throws CsiiException {
  
  int maxUpLoadFileSize = 1024*1024*10;//10M
  File f = getUpLoadFile("UploadFile") ;
  if(f.exists()){
   System.out.println("文件存在");
   System.out.println("文件大小为:"+f.length()/1024+"KB"); 
   long aaa= 1024;
 if(f.length()/1024>=aaa){
   throw new CsiiInputException("文件大小不能超过1M");
 }

  } else{
   try {
    f.createNewFile();
   } catch (IOException e1) {
    e1.printStackTrace();
   }
   System.out.println("文件不存在");
   throw new CsiiInputException("文件不存在,请选择要上传的文件");
  }
  
  File upFile = getUpLoadFile("UploadFile");
  byte fileByte[] = new byte[Integer.parseInt(Long.toString(upFile.length()))];
  /* 判断文件类型*/
  String fileProper = upFile.getName().substring(upFile.getName().lastIndexOf("."));
  System.err.println("fileProper" + fileProper);
  IndexedData resultD = new IndexedData();
  try {
    if (fileProper.equals(".rar")) {
   /* 解析excle */
   System.err.println("fileProper" + fileProper);
    }
    else {
   throw new CsiiInputException("上传文件格式不对,请检查是否为rar文件");
    }
  }
  catch (Exception e) {
    throw new CsiiInputException("文件只能上传(.rar)格式文件,请检查上传文件");
  }
  
 }

/**
 * 得到上传后存放在本地的文件名。
 */
protected java.io.File getUpLoadFile(String name) {
 return MultipartUtil.getUpLoadFile(getParameterValues(name));
}

/**
 * 根据Multipart返回的内容得到在server上存储的相应的文件。
 * @return java.io.File
 * @param values java.lang.String[]
 */
public static java.io.File getUpLoadFile(String[] values) {
 File file = null;
 if ( values != null && values.length == 5 ) {
  if ( values[0].equals("MultiPartFile") ) {
   StringBuffer filename = new StringBuffer(values[1]); // values[1] is the directory name.
   filename.append(File.separator);
   filename.append(values[3]);                          // values[3] is the digest.
   filename.append(values[2]);                          // values[2] is the file name.
   file = new File(filename.toString());
  }
 }
 return file;
 

原创粉丝点击