本文目錄一覽:
JSP上傳壓縮包和提取壓縮包的方法?
首先,要上傳,必須建立一個標籤並且在form中input type=”file” name=”myFile”
接收這個標籤中的值的時候,你要在後台JAVA類中定義一個熟悉,比如:private File myFile;
要是你用的servlet這樣接收:myFile = getParmeter(“myFile”);
要是Struts1你要在一個ActionForm中定義private File myFile;並且GET/SET
要是struts2你要在Action類中定義private File myFile;並且GET/SET
OK現在是實現上傳了代碼如下(按照參數就傳值):
/**
*
* @param file 文件
* @param max_length 最大
* @param allowed_types 類型
* @param path 路徑
* @param fileName 文件名
* @param prName 前綴
* @return
*/
public String uploadFile(File file, Integer max_length, String allowed_types, String path, String fileName, String prName){
//這裡是獲取訪問路徑這裡是struts2的方式,struts和servlet是另外中(好像是)
String root = ServletActionContext.getServletContext().getRealPath(path);
try {
if(file == null)
return “”;
if(max_length file.length() )
return “”;
String fileExt = this.getFileExt(fileName);
if( ! allowed_types.contains(fileExt))
return “”;
File filePath = new File(root);
if ( ! filePath.exists()) {
filePath.mkdirs();
}
//System.out.println(fileName);
fileName = prName + System.nanoTime() + “.” + fileExt;
String rootFileName = root + “/” + fileName;
this.writeFile(file, rootFileName);
return path + “/” + fileName;
} catch (Exception e) {
e.printStackTrace();
return “”;
}
}
//以上就是把文件寫入磁盤,也就是所謂的上傳
下載壓縮包解壓的方法:
public void unZipFile() throws UnZipException{
try{
//ZipFile zipFile = new ZipFile(path);
BufferedOutputStream bos = null;
FileInputStream fis = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null){
byte[] data = new byte[BUFFER];
int length=0;
//如果是文件夾就創建它
if(entry.isDirectory()){
String dirName=entry.getName();
String dirPath=targetPath+”/”+dirName;
FileOperate fo = new FileOperate();
fo.createFolder(dirPath);
}
else{
FileOutputStream fos = new FileOutputStream(targetPath+”/”+entry.getName());
bos = new BufferedOutputStream(fos,BUFFER);
while((length=zis.read(data, 0, BUFFER)) != -1){
bos.write(data,0,length);
}
bos.flush();
bos.close();
fos.flush();
fos.close();
}
}
fis.close();
zis.close();
//zipFile.close();
}catch(ZipException e){
e.printStackTrace();
throw new UnZipException(“不是有效的zip文件! “+e.getMessage());
}catch(IOException e){
e.printStackTrace();
throw new UnZipException(“文件讀取錯誤!”+e.getMessage());
}
}
JSP文件里大量JS代碼,想發布時壓縮JS怎麼辦
我是這樣的,用一個插件保存的時候,自動生成一個壓縮文件(.min.js),這樣頁面直接引用.min.js,而需要調試的時候改成.js就好了。
也有很多打包工具可以幫你做這些事,主要是看你的開發環境
怎樣壓縮html或者jsp文件中標記元素之間的空格
有專門的jsp壓縮工具,下載個來壓縮就行了。html文件不大,不影響速度,用Dreamweaver 處理下就行了。
推薦工具 js壓縮專家 JsPacker 1.0
原創文章,作者:GJ0ZN,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129160.html