struts2 word、excel文件的上传。用到RandomAccessFile 类读取文件。
2013-03-01 10:20
429 查看
java se基础部分。( 获取文件的名称,记录下来以后用到的时候再用。。。。)
==============================================================================================
public class A {
static HashMap<String, String> fileList=new HashMap<String, String>();
public static void find(String path){
File file=new File(path);
File[] files=file.listFiles();
if(files==null)
return;
for(int i=0;i<files.length;i++){
//判断是不是文件夹
if(files[i].isDirectory()){
//递归方法,是文件夹继续下一层的查找
find(files[i].getAbsolutePath());
}else {
//记录文件的路径
String filePath=files[i].getAbsolutePath().toLowerCase();
String fileName=files[i].getName().toLowerCase();
获取所有文件夹下所有文件的名称。
System.out.println(fileName);
fileList.put(fileName, filePath);
}
}
}
public static void main(String[] args) {
String path="D:/新しいフォルダ";
find(path);
}
}
==================================================================================================
ssh文件的上传
一、文件的上传。
struts2的ActionSupport类封装了三个属性。
File XX 文件流
String XXFileName 文件名称
String XXContentType 文件类型
jsp页面:
<s:file name="upload" ></s:file>设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传
<s:form action="ResumeAdd.action" method="post" enctype="multipart/form-data"></s:form>
因为name="upload"
所以后台action中定义如下:
一定要注意大小写。。。。。。。
private File upload;
private String uploadFileName;
private String uploadContentType;
此处省略set get 方法。。。。。。。。
因为数据库中存储的是文件的名称。所以首先要得到文件的名称。getUpload()得到File文件。那么是不是getUpload().getName就能得到文件名称吗?答案是否定的。这样得到的是缓存文件的文件名称。(第一次用ssh做文件上传下载时。不知道action封装了FileName属性。因此做到这边时我就难住我了。知道后。。。)。
得到文件的名称:
String fileName=this.getUploadFileName().toLowerCase();
判断文件是否是Word或者Excel(判断可以再前台用js些。但是后台做好也写这个判断。因为当你获取的文件名称是乱码时前台不能够检测到,到后天进行检测。这个我碰到过的。。)
if(!fileName.endsWith(".doc")&&!fileName.endsWith(".xls")){
resume.setMessage (【出现错误时的提示信息】);
}
获取文件路径
String pathname=文件存储路径;
boolean bb=true;
File work_path=new File(pathname);
判断文件路径是否存在
if(!work_path.exists()){
如果文件路径不存在则创建路径文件。
bb=work_path.mkdirs();
}
读取文件
InputStream isInputStream=new FileInputStream(this.getUpload());
byte[] bytes = new byte[isInputStream.available()];
int length=isInputStream.read(bytes);
RandomAccessFile out=new RandomAccessFile(文件路径+文件名称, "rw");
关于RandomAccessFile的具体的使用可以参照jdk的帮助文档。
out.write(bytes);
isInputStream.close();
out.close();
以上就是我在做文件上传时遇到的一些问题。。如有疑问欢迎提出。。
==============================================================================================
public class A {
static HashMap<String, String> fileList=new HashMap<String, String>();
public static void find(String path){
File file=new File(path);
File[] files=file.listFiles();
if(files==null)
return;
for(int i=0;i<files.length;i++){
//判断是不是文件夹
if(files[i].isDirectory()){
//递归方法,是文件夹继续下一层的查找
find(files[i].getAbsolutePath());
}else {
//记录文件的路径
String filePath=files[i].getAbsolutePath().toLowerCase();
String fileName=files[i].getName().toLowerCase();
获取所有文件夹下所有文件的名称。
System.out.println(fileName);
fileList.put(fileName, filePath);
}
}
}
public static void main(String[] args) {
String path="D:/新しいフォルダ";
find(path);
}
}
==================================================================================================
ssh文件的上传
一、文件的上传。
struts2的ActionSupport类封装了三个属性。
File XX 文件流
String XXFileName 文件名称
String XXContentType 文件类型
jsp页面:
<s:file name="upload" ></s:file>设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传
<s:form action="ResumeAdd.action" method="post" enctype="multipart/form-data"></s:form>
因为name="upload"
所以后台action中定义如下:
一定要注意大小写。。。。。。。
private File upload;
private String uploadFileName;
private String uploadContentType;
此处省略set get 方法。。。。。。。。
因为数据库中存储的是文件的名称。所以首先要得到文件的名称。getUpload()得到File文件。那么是不是getUpload().getName就能得到文件名称吗?答案是否定的。这样得到的是缓存文件的文件名称。(第一次用ssh做文件上传下载时。不知道action封装了FileName属性。因此做到这边时我就难住我了。知道后。。。)。
得到文件的名称:
String fileName=this.getUploadFileName().toLowerCase();
判断文件是否是Word或者Excel(判断可以再前台用js些。但是后台做好也写这个判断。因为当你获取的文件名称是乱码时前台不能够检测到,到后天进行检测。这个我碰到过的。。)
if(!fileName.endsWith(".doc")&&!fileName.endsWith(".xls")){
resume.setMessage (【出现错误时的提示信息】);
}
获取文件路径
String pathname=文件存储路径;
boolean bb=true;
File work_path=new File(pathname);
判断文件路径是否存在
if(!work_path.exists()){
如果文件路径不存在则创建路径文件。
bb=work_path.mkdirs();
}
读取文件
InputStream isInputStream=new FileInputStream(this.getUpload());
byte[] bytes = new byte[isInputStream.available()];
int length=isInputStream.read(bytes);
"rw" | 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。 |
关于RandomAccessFile的具体的使用可以参照jdk的帮助文档。
out.write(bytes);
isInputStream.close();
out.close();
以上就是我在做文件上传时遇到的一些问题。。如有疑问欢迎提出。。
相关文章推荐
- java中利用RandomAccessFile读取超大文件
- BufferedRandomAccessFile--解决RandomAccessFile读取大文件时效率低的问题
- RandomAccessFile读取文件
- 在文件指定位置读取和写入内容RandomAccessFile
- 随机读取文件--RandomAccessFile的应用实例
- java:RandomAccessFile随机读取文件内容
- 使用RandomAccessFile来读取不断增长的文件
- java 读取文件 RandomAccessFile
- 使用xUtils3和RandomAccessFile来实现文件的分片上传
- RandomAccessFile读取超大文件 随机读一行
- 【幻化万千戏红尘】qianfengDay20-java基础学习:数据流、随机读取文件流RandomAccessFile
- 按行读取文件比较Scanner和RandomAccessFile读取的效率
- RandomAccessFile读取文件阻塞住了怎么回事
- 2011-09-01--RandomAccessFile 随机读取文件
- 使用RandomAccessFile这个类,从文件中读取数据,却读取不出来
- 高效率读取大文件进行数据处理-通过扩展RandomAccessFile类使之具备Buffer改善I/O性能
- 读取文件任意位置的内容——RandomAccessFile
- RandomAccessFile实时读取大文件(转)
- 读取文件任意位置的内容——RandomAccessFile
- Java RandomAccessFile随机读取文件