上传文件 JSP+java
2007-07-09 10:52
267 查看
上传文件 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;
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;
相关文章推荐
- 使用jspsmartupload上传文件抛出的java.lang.IllegalStateException异常
- 文件上传 java jsp
- Web开发(jsp+javaBean+servlet )之文件上传
- java如何获取jsp页面上传的文件路径 20 前台jsp: <form action="AA.do?method=yktAdd" method="post" > <input type='text'
- java的jap页面上文件上传,所需jar包:jspSmartUpload.jar
- 一个JSP上传文件和下载文件的JavaBean
- java鬼混笔记:jsp上传文件不跳转页面
- 自己写的一个JSP上传文件和下载文件的JavaBean 自己写的一个JSP上传文件和下载文件的JavaBean
- Java+jsp+Servlet实现文件上传和下载
- SERVLET与JSP_文件上传下载(4)-java
- jsp文件上传与java文件保存到tomcat
- Java / JSP 文件上传与下载
- 自己写的一个JSP上传文件和下载文件的JavaBean
- Java【代码】使用jspSmartUpload实现文件上传demo
- java /Spring /jsp 使用input标签 file 如何上传多个文件
- java+jsp代码实现从FTP服务器上传下载文件
- JSP/Java/Struts实现文件上传
- java jsp 实现文件上传
- 自己写的一个JSP上传文件和下载文件的JavaBean
- VC调用jsp上传文件到JAVA后台代码