📄 fileutil.java
字号:
package com.singnet.util;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.upload.FormFile;
public class FileUtil {
public FileUtil() {
}
private String allowext="";//允许的格式
private int allowsize=0;//允许的大小
private String filepath="";//上传到的目录
public void setAllowext(String allowext)
{
this.allowext=allowext;
}
public void setAllowsize(int allowsize)
{
this.allowsize=allowsize;
}
public String getAllowext()
{
return allowext;
}
public int getAllowsize()
{
return allowsize;
}
/**
* 创建目录
* @return boolean
*/
public static boolean MakeDir(String dirpath) {
boolean returnValue = false;
java.io.File myFilePath = new java.io.File(dirpath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
returnValue = true;
}
return returnValue;
}
/**
* 创建文件
* @param filepath String
* @return boolean
*/
public static boolean CreateFile(String filepath) {
boolean returnValue = false;
try {
java.io.File myFilePath = new java.io.File(filepath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
returnValue = true;
}
} catch (Exception e) {
returnValue = false;
e.printStackTrace();
}
return returnValue;
}
/**
* 获取文件格式
*
*/
public static String getFileext(String filename) {
return filename.substring(filename.lastIndexOf("."));
}
/**
* 判断格式是否允许
*
*/
public static boolean checkFileext(String allowext, String fileext) {
boolean re=false;
if (allowext.indexOf(fileext) >=0) {
re=true;
}
return re;
}
/**
*上传文件程序,还没有完善
*/
public static boolean doUpload(FormFile file, HttpServletRequest request,
HttpServletResponse response, String dir,String filename) {
try {
InputStream stream = file.getInputStream(); //把文件读入
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(dir + filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead); //将文件写入服务器
}
bos.close();
stream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -