① linux下的java通過ftp讀取另一linux下的文件名出現中文亂碼。
需要轉一下編碼,你的java的class文家中是GBK的編碼,對面linux下是iso8859-1編碼
String fileNameTmp = new String(files[i].getBytes("iso-8859-1"), "GBK");//將從linux取得的內文件名轉容換為GBK編碼
String filename=fileNameTmp .substring(regStr.length()+1,fileNameTmp .length());
然後再把轉完編碼的文件名按你的要求進行截取
② linux伺服器上部署java項目,本地windos通過瀏覽器訪問項目怎麼下載項目目錄下的文件到本
既然使用了java,實現這種功能就與OS無關了,否則叫什麼跨平台。其實用瀏覽器下載伺服器端文件比較容易:
首先,要讓用戶能找到並選擇文件(jsp里實現,部分代碼)
String realPath=request.getSession().getServletContext().getRealPath("")+"/documents";//項目根目錄下文件路徑
File fileDir=new File(realPath);
String[] fileList=fileDir.list();//返回目錄下文件名稱數組
for(int i=0;i<fileList.length;i++){
//這里遍歷出來要顯示的文件名,加到td里,後面再加上個「下載」按鈕
//使用隱藏input記錄文件名和路徑fileName,filePath
}
其次,提交下載請求並下載
使用form提交用戶選擇的文件名,Action中部分代碼:
String fileName=req.getParameter("fileName");//HttpServletRequest req
String filePath=req.getParameter("filePath");
try {
FileDownload.Download(filePath+"/"+fileName, "attachment", res);
} catch (Exception e) {
e.printStackTrace();
}
下面是 FileDownload類:
package com.aerolink.aocs.util.fileUtil;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
/**
* <p>
* Title: FileDownload類
* </p>
* <p>
* Description: 實現文件下載功能
* </p>
* <p>
* 將文件名,HttpServletRequest,HttpServletRespons傳給靜態方法Download即可
* </p>
* <p>
* Copyright: Copyright (c) 2005
* </p>
* <p>
* Company: 北京天航信達信息技術有限公司
* </p>
*
* @author 陶源
* @version 2.0
*/
public class FileDownload {
/**
* @param fileName
* @param res
* @throws FileNotFoundException
* @throws IOException
*/
public static void Download(String fileName,
HttpServletResponse res)
throws FileNotFoundException, IOException {
String fileContentType = "application/octet-stream";
String fileDownloadType = "attachment";
long totalsize = 0;
// 取得要傳輸的文件,實際應用是可以將文件路徑以參數的形式傳入
File f = new File(fileName);
// 取文件長度
long filelength = f.length();
byte[] b = new byte[1024];
// 設置文件輸出流
FileInputStream fin = new FileInputStream(f);
DataInputStream in = new DataInputStream(fin);
int pos = fileName.lastIndexOf(java.io.File.separator);
String fn = new String(fileName.substring(pos + 1).getBytes("gb2312"),
"ISO8859-1");
// 設置相應頭信息,讓下載的文件顯示保存信息
res.setContentType(fileContentType);
res.setHeader("Content-Disposition", fileDownloadType + ";filename=\""
+ fn + "\"");
// 確定長度
String filesize = Long.toString(filelength);
// 設置輸出文件的長度
res.setHeader("Content-Length", filesize);
// 取得輸出流
ServletOutputStream servletOut = res.getOutputStream();
// 發送文件數據,每次1024位元組,最後一次單獨計算
while (totalsize < filelength) {
totalsize += 1024;
if (totalsize > filelength) {
// 最後一次傳送的位元組數
byte[] leftpart = new byte[1024 - (int) (totalsize - filelength)];
// 讀入位元組數組
in.readFully(leftpart);
// 寫入輸出流
servletOut.write(leftpart);
} else {
// 讀入1024個位元組到位元組數組 b
in.readFully(b);
// 寫和輸出流
servletOut.write(b);
}
}
servletOut.close();
}
/**
* @param fileName
* @param fileDownloadType
* @param res
* @throws FileNotFoundException
* @throws IOException
*/
public static void Download(String fileName, String fileDownloadType,
HttpServletResponse res)
throws FileNotFoundException, IOException {
String fileContentType = null;
if (fileName.endsWith(".doc")) {
fileContentType = "application/msword";
} else if (fileName.endsWith(".pdf")) {
fileContentType = "application/pdf";
} else if (fileName.endsWith(".xls")) {
fileContentType = "application/vnd-ms-excel";
} else if (fileName.endsWith(".txt")) {
fileContentType = "text/plain";
} else {
fileContentType = "application/octet-stream";
}
long totalsize = 0;
// 取得要傳輸的文件,實際應用是可以將文件路徑以參數的形式傳入
File f = new File(fileName);
// 取文件長度
long filelength = f.length();
byte[] b = new byte[1024];
// 設置文件輸出流
FileInputStream fin = new FileInputStream(f);
DataInputStream in = new DataInputStream(fin);
int pos = fileName.lastIndexOf(java.io.File.separator);
String fn = new String(fileName.substring(pos + 1).getBytes("gb2312"),
"ISO8859-1");
// 設置相應頭信息,讓下載的文件顯示保存信息
res.setContentType(fileContentType);
res.setHeader("Content-Disposition", fileDownloadType + ";filename=\""
+ fn + "\"");
// 確定長度
String filesize = Long.toString(filelength);
// 設置輸出文件的長度
res.setHeader("Content-Length", filesize);
// 取得輸出流
ServletOutputStream servletOut = res.getOutputStream();
// 發送文件數據,每次1024位元組,最後一次單獨計算
while (totalsize < filelength) {
totalsize += 1024;
if (totalsize > filelength) {
// 最後一次傳送的位元組數
byte[] leftpart = new byte[1024 - (int) (totalsize - filelength)];
// 讀入位元組數組
in.readFully(leftpart);
// 寫入輸出流
servletOut.write(leftpart);
} else {
// 讀入1024個位元組到位元組數組 b
in.readFully(b);
// 寫和輸出流
servletOut.write(b);
}
}
servletOut.close();
}
}
③ java如何拼接linux目錄下文件路徑
您好,提問者:
使用StringBuilder的append方法進行拼接,例如:
StringBuilderfilePath=newStringBuilder(request.getSession().getServletContext().getRealPath("/"));
Stringpath="/upload/data/xxxx.doc";
if("/".equals(File.separator)){
//fileAllPath="";//這個路徑該如何拼內接
filePath.append("xx");
filePath.append("xxxx");
}
//輸出容的時候直接輸出filePath.toString();
④ 如何用java獲取linux下某文件夾的大小
通過FileInputStream來獲取的文件大小:
public static void main(String[] args) {
FileInputStream fis= null;
try{
File f= new File("D:\\CentOS-6.5-x86_64-bin-DVD1.iso");
fis= new FileInputStream(f);
logger.info(fis.available());
}catch(Exception e){
logger.error(e);
} finally{
if (null!=fis){
try {
fis.close();
} catch (IOException e) {
logger.error(e);
}
}
}
}
下面是運行結果:
2147483647
它是Integer.MAX_VALUE,也就是有符號整型能表示的最大數值。