java代码实现文件阅览 java读文件代码

一个浏览上传多个文件的java代码在Java中可以用struts2实现多个文件同时上传代码,功能代码如下:
1、upload.jsp页面(选择上传文件)
form action="upload.action" name="uploadForm" method="post" enctype="multipart/form-data"
文件标题:input type="text" name="title"/br/
选择文件-:input type="file" name="upload"/br/
选择文件二:input type="file" name="upload"/br/
选择文件三:input type="file" name="upload"/br/
input type="submit" value="https://www.04ip.com/post/upload"/
/form
2、action代码如下:
//对应的Action依次遍历所有文件域,然后生成对应的输入文件流,输出文件流在指定的服务器保存路径中添加对应的输出文件流保存文件 。同时动态指定服务器上文件的保存路径 。
package com.inspur.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File[] upload;
private String[] uploadFileName;
private String[] uploadContentType;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload()throws Exception{
File[] files=this.getUpload();
for(int i=0;ifiles.length;i){
FileOutputStream fos=new FileOutputStream(this.getSavePath() "\\" this.getUploadFileName()[i]);
byte[] buffer=new byte[1024];
FileInputStream fis=new FileInputStream(files[i]);
int len=0;
while((len=fis.read(buffer))0){
fos.write(buffer,0,len);
}
}
return SUCCESS;
}
}
3、success.jsp页面代码如下(上传成功界面显示所有上传的图片)
文件标题:s:property value="https://www.04ip.com/post/title"/br/
第一个图片:img alt="first" src="https://www.04ip.com/post/s:property value="'upload/' uploadFileName[0]"/"/br/
第二个图片:img alt="second" src="https://www.04ip.com/post/s:property value="'upload/' uploadFileName[1]"/"/br/
java实现文件预览就像百度文库那样的文件夹:build-存放编译后的class文件
nbproject-存放项目的具体配置文件
src-java源代码存放文件夹
test-JUnit测试文件存放位置
Build.xml构建描述文件 , 因为Netbeans的编译打包是基于ant的,build就是ant实现自动编译打包的描述文件 。
manifest.mf是打包的时候需要的一个清单文件 , 是对打包后的jar包中的文件的一个描述下文件 。
original.java,.netbeans_automatic_build,这两个文件不知道你用的什么Netbeans版本,在我的版本中并未生成此文件 。
build-impl.xml是对Build.xml文件的具体描述 , Build.xml其实并未实现具体的ant构建脚本,具体是在该文件中实现的,如果你看过build.xml的源代码,就会发现一句import file="nbproject/build-impl.xml"/ , 这你就知道是什么意思了 。
怎么用java代码读取excel文件本例使用java来读取exceljava代码实现文件阅览的内容并展出出结果java代码实现文件阅览,代码如下:
复制代码 代码如下:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExcelOperate {
public static void main(String[] args) throws Exception {
File file = new File("ExcelDemo.xls");
String[][] result = getData(file, 1);
int rowLength = result.length;
for(int i=0;irowLength;i) {
for(int j=0;jresult[i].length;j) {
System.out.print(result[i][j] "\t\t");
}
System.out.println();
}
}
/**
* 读取Exceljava代码实现文件阅览的内容,第一维数组存储的是一行中格列的值,二维数组存储的是多少个行
* @param file 读取数据的源Excel
* @param ignoreRows 读取数据忽略的行数,比喻行头不需要读入 忽略的行数为1
* @return 读出的Excel中数据的内容
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
ListString[] result = new ArrayListString[]();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打开HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndexwb.getNumberOfSheets(); sheetIndex) {
HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行为标题,不取
for (int rowIndex = ignoreRows; rowIndex = st.getLastRowNum(); rowIndex) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum()1;
if (tempRowSizerowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = https://www.04ip.com/post/false;
for (short columnIndex = 0; columnIndex = row.getLastCellNum(); columnIndex) {
String valuehttps://www.04ip.com/post/= "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要设成这个,否则可能会出现乱码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = https://www.04ip.com/post/cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = https://www.04ip.com/post/new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
valuehttps://www.04ip.com/post/= "";
}
} else {
value = https://www.04ip.com/post/new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
value = https://www.04ip.com/post/cell.getStringCellValue();
} else {
value = https://www.04ip.com/post/cell.getNumericCellValue()"";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
valuehttps://www.04ip.com/post/= "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = https://www.04ip.com/post/(cell.getBooleanCellValue() == true ?"Y"
: "N");
break;
default:
valuehttps://www.04ip.com/post/= "";
}
}
if (columnIndex == 0value.trim().equals("")) {
break;
}
values[columnIndex] = rightTrim(value);
hasValue = https://www.04ip.com/post/true;
}
java实现动态读取文件夹文件信息java动态读取某个文件夹下的所有文件信息,代码如下:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
public class ReadFile {
public ReadFile() {
}
/**
* 动态读取某个文件夹下的所有文件信息
*/
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path="file.getPath());
System.out.println("absolutepath="file.getAbsolutePath());
System.out.println("name="file.getName());
} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; ifilelist.length; i) {
File readfile = new File(filepath"\\"filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path="readfile.getPath());
System.out.println("absolutepath="
readfile.getAbsolutePath());
System.out.println("name="readfile.getName());
} else if (readfile.isDirectory()) {
readfile(filepath"\\"filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile()Exception:"e.getMessage());
}
return true;
}
public static void main(String[] args) {
try {
readfile("e:/videos");
// deletefile("D:/file");
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
System.out.println("ok");
}
}
【java代码实现文件阅览 java读文件代码】关于java代码实现文件阅览和java读文件代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读