JSP如何创建文件 怎么能够弹出文件浏览器,并产生一个新文件呀,急 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package diyweb.common.util;import java.io.*;import java.util.*;/** * 文件管理 * @author xiejh */public class FileManage { public FileManage() { } /** * 复制文件 * @param oldPath String 被复制的文件 * @param newPath String 复制的文件 * @return -1表示失败,0表示已经存在文件,1表示复制成功 */ public int copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); File newfile = new File(newPath); if (oldfile.exists()) { // 文件存在时 if (newfile.exists()) { return 0; } else { InputStream inStream = new FileInputStream(oldPath); // 读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; // 字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); fs.close(); } } return 1; } catch (Exception e) { System.out.println("复制单个文件操作出错"); e.printStackTrace(); return -1; } } /** * 删除文件 * @param filePathAndName 文件路径 * @return 操作结果 */ public String delFile(String filePathAndName) { String strresult = ""; try { String filePath = filePathAndName; filePath = filePath.toString(); File myDelFile = new File(filePath); if (myDelFile.exists()) { boolean result = myDelFile.delete(); if (result) { strresult = "删除文件成功!"; } else { strresult = "删除文件操作失败!"; } } else { strresult = "文件不存在"; } } catch (Exception e) { strresult = "删除文件操作出错"; e.printStackTrace(); } return strresult; } /** * 得到文件目录下的所有文件及文件夹 * @param path String 路径 * @return ArrayList 文件及文件夹列表 */ public ArrayList getAllFilesInPath(String path) { ArrayList alAllFile = new ArrayList(); File file = new File(path); if (!file.exists()) { return null; } File[] aryFile = file.listFiles(); for (int i = 0; i < aryFile.length; i++) { alAllFile.add(aryFile[i]); } return alAllFile; } /** * 得到文件目录下的所有文件 * @param path 路径 * @return 文件列表 */ public ArrayList getAllFileInPath(String path) { ArrayList alAllFile = new ArrayList(); File file = new File(path); if (!file.exists()) { return null; } if (!file.isDirectory()) { return null; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { alAllFile.add(temp); } } return alAllFile; } /** * 得到文件夹下的所有文件名 * @param path 路径 * @return 文件名列表 */ public ArrayList getAllFileNameInPath(String path) { ArrayList alAllFile = new ArrayList(); File file = new File(path); if (!file.exists()) { return null; } if (!file.isDirectory()) { return null; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { alAllFile.add(temp.getName()); } } return alAllFile; } /** * 在文件夹中是否存在某一文件 * @param path 路径 * @param filename 文件名 * @return boolean true 存在该文件 false 不存在该文件 */ public boolean ifExistFileNameInPath(String path, String filename) { boolean haveFile = false; ArrayList alAllFile = getAllFileNameInPath(path); if (alAllFile != null && alAllFile.size() > 0) { for (int i = 0; i < alAllFile.size(); i++) { if (alAllFile.get(i).toString().equals(filename)) { haveFile = true; return haveFile; } } } return haveFile; } /** * 读文件内容 * @param strFilePath 文件路径 * @return 文件内容 * @throws IOException 异常 */ public String readFile(String strFilePath) throws IOException { String strReturn = ""; StringBuffer strline = new StringBuffer(); BufferedReader reader = null; File file = null; FileReader freader = null; try { file = new File(strFilePath);// 需要指定文件路径 freader = new FileReader(file); reader = new BufferedReader(freader); String strtemp = ""; strtemp = reader.readLine(); while (strtemp != null) { strtemp = reader.readLine(); strline.append(strtemp + "\n"); // name } } catch (FileNotFoundException e) { System.err.println("没有找到文件"); } strReturn = strline.toString(); return strReturn; } /** * 取得上级目录 * @param strFilePath 目录路径 * @return 上级目录 */ public String getUpFoldPath(String strFilePath) { int pos = 0; pos = strFilePath.lastIndexOf('/'); if (pos != -1) { return strFilePath.substring(0, pos); } pos = strFilePath.lastIndexOf('\\'); if (pos != -1) { return strFilePath.substring(0, pos); } else { return strFilePath; } } /** * 取得文件的后缀名 * @param fileName 文件名 * @return 文件的后缀名 */ public String getFileSuffix(String fileName) { int pos = 0; pos = fileName.lastIndexOf('.'); if (pos != -1) { return fileName.substring(pos + 1); } else { return ""; } } /** * 创建文件夹 * @param foldPath 路径 * @return 文件夹名 */ public String creatFlod(String foldPath) { File f = new File(foldPath); if (f.exists()) { } else { f.mkdirs(); } return f.getName(); }} package com.nomen.common;import java.io.*;public class PubFile{ public PubFile(){ } //输出提定目录下的文件名及文件夹名;path为文件目录 public String[] getFileName(String path){ File f1 = new File(path); String[] filename; String[] aa = {}; if(f1.isDirectory()){ String s[] = f1.list(); filename = new String[s.length]; for(int i = 0;i < s.length; i++){ File f = new File(path+"/"+s[i]); if(f.isDirectory()){ filename[i] = "|"+s[i]; }else{ filename[i] = "?"+s[i]; } } return filename; }else{ return aa; } } public boolean isBeenByDir(String dir){ File f1 = new File(dir); return f1.isDirectory(); } public boolean DelDir(String dir){ PubFile file = new PubFile(); File f1 = new File(dir); String s[] = f1.list(); try{ for(int i=0;i<s.length;i++){ File f =new File(dir+"/"+s[i]); if(f.isDirectory()){ file.DelDir(dir+"/"+s[i]); f.delete(); }else{ f.delete(); } } f1.delete(); return true; }catch(Exception e){ return false; } } //获取文件大少,path为文件地址 public int getfileSize(String path){ File f = new File(path); if(f.isFile()){ try{ InputStream file = new FileInputStream(path); return file.available(); }catch(Exception e){ return 0; } }else{ return 0; } } //获取文件内容,path为文件地址 public byte[] getFileContent(String path){ File f = new File(path); byte[] s={}; PubFile file = new PubFile(); if(f.isFile()){ try{ InputStream f0 = new FileInputStream(path); int n = file.getfileSize(path); byte[] str = new byte[n]; for(int i = 0 ; i < n ; i++){ str[i]=(byte) f0.read(); } return str; }catch(Exception e){ return s; } }else{ return s; } } //获取文件内容,path为文件地址 public String getFileContent2(String path){ File f = new File(path); String s=""; PubFile file = new PubFile(); if(f.isFile()){ try{ InputStream f0 = new FileInputStream(path); int n = file.getfileSize(path); String str = ""; for(int i = 0 ; i < n ; i++){ str+=(char) f0.read(); } return file.toGb(str); }catch(Exception e){ return s; } }else{ return s; } } //创建文件夹,path为文件夹地址 public boolean createDir(String path) { File f1 = new File(path); return f1.mkdir(); } //创建文件,str为该文件内容,path为文件目录,fileName为该文件文件名 public void createFile(byte[] str,String path,String filename){ try{ OutputStream newfile = new FileOutputStream(path+"/"+filename); for(int i=0;i<str.length;i++){ newfile.write(str[i]); } newfile.close(); }catch(Exception e){ //System.out.println(e.toString()); } } //复制文件 public void copyFile(String path1,String path2,String filename1,String filename2){ File f = new File(path1+"/"+filename1); PubFile file = new PubFile(); if(f.isFile()){ try{ InputStream f0 = new FileInputStream(path1+"/"+filename1); OutputStream newfile = new FileOutputStream(path2+"/"+filename2); int n = f0.available(); byte[] str = new byte[n]; for(int i = 0 ; i < n ; i++){ newfile.write((byte) f0.read()); } }catch(Exception e){ } } } //汉字字符编码方式处理 uniStr为要处理的字符 public String toGb(String uniStr){ String gbStr = ""; if(uniStr == null){ uniStr = ""; } try{ byte[] tempByte = uniStr.getBytes("ISO8859_1"); gbStr = new String(tempByte,"GB2312"); }catch(Exception ex){ } return gbStr; } //gbStr 为要处理的字符 public String toUni(String gbStr){ String uniStr = ""; if(gbStr == null){ gbStr = ""; } try{ byte[] tempByte = gbStr.getBytes("GB2312"); uniStr = new String(tempByte,"ISO8859_1"); }catch(Exception ex){ } return uniStr; } /*public static void main(String args[]){ PubFile file = new PubFile(); file.createDir("F:/JSP/cai/aa"); }*/ }自已写的,看下能不能用得到 在java中statement.getUpdateCount()的返回值是-1 帮忙写这个正则 探讨实现方法:网页以类似于WORD文档的文档结构图来展现 奇怪的网页问题(向电脑高手请教答案) Servlet 创建MYSQL数据库出现错误 下拉列表框的颜色控制 在每次进入网页的时候,如何把用户表中的用户名字段动态地加入到一个下拉列表框中? apache + jsp 高分请教两个问题 JPA自关联怎么设置 关于分页查询的问题,请高手赐教 读取数据,显示
import java.util.*;/**
* 文件管理
* @author xiejh
*/
public class FileManage {
public FileManage() {
} /**
* 复制文件
* @param oldPath String 被复制的文件
* @param newPath String 复制的文件
* @return -1表示失败,0表示已经存在文件,1表示复制成功
*/
public int copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
File newfile = new File(newPath);
if (oldfile.exists()) {
// 文件存在时
if (newfile.exists()) {
return 0;
} else {
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
}
return 1;
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
return -1;
}
} /**
* 删除文件
* @param filePathAndName 文件路径
* @return 操作结果
*/
public String delFile(String filePathAndName) {
String strresult = "";
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myDelFile = new File(filePath);
if (myDelFile.exists()) {
boolean result = myDelFile.delete();
if (result) {
strresult = "删除文件成功!";
} else {
strresult = "删除文件操作失败!";
}
} else {
strresult = "文件不存在";
}
} catch (Exception e) {
strresult = "删除文件操作出错";
e.printStackTrace();
}
return strresult;
} /**
* 得到文件目录下的所有文件及文件夹
* @param path String 路径
* @return ArrayList 文件及文件夹列表
*/
public ArrayList getAllFilesInPath(String path) {
ArrayList alAllFile = new ArrayList();
File file = new File(path);
if (!file.exists()) {
return null;
}
File[] aryFile = file.listFiles();
for (int i = 0; i < aryFile.length; i++) {
alAllFile.add(aryFile[i]);
}
return alAllFile;
} /**
* 得到文件目录下的所有文件
* @param path 路径
* @return 文件列表
*/
public ArrayList getAllFileInPath(String path) {
ArrayList alAllFile = new ArrayList();
File file = new File(path);
if (!file.exists()) {
return null;
}
if (!file.isDirectory()) {
return null;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
alAllFile.add(temp);
}
}
return alAllFile;
} /**
* 得到文件夹下的所有文件名
* @param path 路径
* @return 文件名列表
*/
public ArrayList getAllFileNameInPath(String path) {
ArrayList alAllFile = new ArrayList();
File file = new File(path);
if (!file.exists()) {
return null;
}
if (!file.isDirectory()) {
return null;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
alAllFile.add(temp.getName());
}
}
return alAllFile;
} /**
* 在文件夹中是否存在某一文件
* @param path 路径
* @param filename 文件名
* @return boolean true 存在该文件 false 不存在该文件
*/
public boolean ifExistFileNameInPath(String path, String filename) {
boolean haveFile = false;
ArrayList alAllFile = getAllFileNameInPath(path);
if (alAllFile != null && alAllFile.size() > 0) {
for (int i = 0; i < alAllFile.size(); i++) {
if (alAllFile.get(i).toString().equals(filename)) {
haveFile = true;
return haveFile;
}
}
}
return haveFile;
} /**
* 读文件内容
* @param strFilePath 文件路径
* @return 文件内容
* @throws IOException 异常
*/
public String readFile(String strFilePath) throws IOException {
String strReturn = "";
StringBuffer strline = new StringBuffer();
BufferedReader reader = null;
File file = null;
FileReader freader = null;
try {
file = new File(strFilePath);// 需要指定文件路径
freader = new FileReader(file);
reader = new BufferedReader(freader);
String strtemp = "";
strtemp = reader.readLine(); while (strtemp != null) {
strtemp = reader.readLine();
strline.append(strtemp + "\n"); // name
}
} catch (FileNotFoundException e) {
System.err.println("没有找到文件");
}
strReturn = strline.toString();
return strReturn;
} /**
* 取得上级目录
* @param strFilePath 目录路径
* @return 上级目录
*/
public String getUpFoldPath(String strFilePath) {
int pos = 0;
pos = strFilePath.lastIndexOf('/');
if (pos != -1) {
return strFilePath.substring(0, pos);
}
pos = strFilePath.lastIndexOf('\\');
if (pos != -1) {
return strFilePath.substring(0, pos);
} else {
return strFilePath;
}
} /**
* 取得文件的后缀名
* @param fileName 文件名
* @return 文件的后缀名
*/
public String getFileSuffix(String fileName) {
int pos = 0;
pos = fileName.lastIndexOf('.');
if (pos != -1) {
return fileName.substring(pos + 1);
} else {
return "";
}
} /**
* 创建文件夹
* @param foldPath 路径
* @return 文件夹名
*/
public String creatFlod(String foldPath) {
File f = new File(foldPath);
if (f.exists()) {
} else {
f.mkdirs();
}
return f.getName();
}
}
public class PubFile{
public PubFile(){
}
//输出提定目录下的文件名及文件夹名;path为文件目录
public String[] getFileName(String path){
File f1 = new File(path);
String[] filename;
String[] aa = {};
if(f1.isDirectory()){
String s[] = f1.list();
filename = new String[s.length];
for(int i = 0;i < s.length; i++){
File f = new File(path+"/"+s[i]);
if(f.isDirectory()){
filename[i] = "|"+s[i];
}else{
filename[i] = "?"+s[i];
}
}
return filename;
}else{
return aa;
}
}
public boolean isBeenByDir(String dir){
File f1 = new File(dir);
return f1.isDirectory();
}
public boolean DelDir(String dir){
PubFile file = new PubFile();
File f1 = new File(dir);
String s[] = f1.list();
try{
for(int i=0;i<s.length;i++){
File f =new File(dir+"/"+s[i]);
if(f.isDirectory()){
file.DelDir(dir+"/"+s[i]);
f.delete();
}else{
f.delete();
}
}
f1.delete();
return true;
}catch(Exception e){
return false;
}
}
//获取文件大少,path为文件地址
public int getfileSize(String path){
File f = new File(path);
if(f.isFile()){
try{
InputStream file = new FileInputStream(path);
return file.available();
}catch(Exception e){
return 0;
}
}else{
return 0;
}
}
//获取文件内容,path为文件地址
public byte[] getFileContent(String path){
File f = new File(path);
byte[] s={};
PubFile file = new PubFile();
if(f.isFile()){
try{
InputStream f0 = new FileInputStream(path);
int n = file.getfileSize(path);
byte[] str = new byte[n];
for(int i = 0 ; i < n ; i++){
str[i]=(byte) f0.read();
}
return str;
}catch(Exception e){
return s;
}
}else{
return s;
}
}
//获取文件内容,path为文件地址
public String getFileContent2(String path){
File f = new File(path);
String s="";
PubFile file = new PubFile();
if(f.isFile()){
try{
InputStream f0 = new FileInputStream(path);
int n = file.getfileSize(path);
String str = "";
for(int i = 0 ; i < n ; i++){
str+=(char) f0.read();
}
return file.toGb(str);
}catch(Exception e){
return s;
}
}else{
return s;
}
}
//创建文件夹,path为文件夹地址
public boolean createDir(String path)
{
File f1 = new File(path);
return f1.mkdir();
}
//创建文件,str为该文件内容,path为文件目录,fileName为该文件文件名
public void createFile(byte[] str,String path,String filename){
try{
OutputStream newfile = new FileOutputStream(path+"/"+filename);
for(int i=0;i<str.length;i++){
newfile.write(str[i]);
}
newfile.close();
}catch(Exception e){
//System.out.println(e.toString());
}
}
//复制文件
public void copyFile(String path1,String path2,String filename1,String filename2){
File f = new File(path1+"/"+filename1);
PubFile file = new PubFile();
if(f.isFile()){
try{
InputStream f0 = new FileInputStream(path1+"/"+filename1);
OutputStream newfile = new FileOutputStream(path2+"/"+filename2);
int n = f0.available();
byte[] str = new byte[n];
for(int i = 0 ; i < n ; i++){
newfile.write((byte) f0.read());
}
}catch(Exception e){
}
}
}
//汉字字符编码方式处理 uniStr为要处理的字符
public String toGb(String uniStr){
String gbStr = "";
if(uniStr == null){
uniStr = "";
}
try{
byte[] tempByte = uniStr.getBytes("ISO8859_1");
gbStr = new String(tempByte,"GB2312");
}catch(Exception ex){
}
return gbStr;
}
//gbStr 为要处理的字符
public String toUni(String gbStr){
String uniStr = "";
if(gbStr == null){
gbStr = "";
}
try{
byte[] tempByte = gbStr.getBytes("GB2312");
uniStr = new String(tempByte,"ISO8859_1");
}catch(Exception ex){
}
return uniStr;
}
/*public static void main(String args[]){
PubFile file = new PubFile();
file.createDir("F:/JSP/cai/aa");
}*/
}
自已写的,看下能不能用得到