创建一个文件,然后在java中删除
我的要求是有一个导出按钮,点击数据库中的数据被加载,然后转换成.csv、.doc或.html文件,可以在这里打开或保存,这需要转换成文件,保存在本地路径中,上传到SFTP服务器,并从loacl路径中删除。我的代码是这样的创建一个文件,然后在java中删除,java,javascript,hibernate,Java,Javascript,Hibernate,我的要求是有一个导出按钮,点击数据库中的数据被加载,然后转换成.csv、.doc或.html文件,可以在这里打开或保存,这需要转换成文件,保存在本地路径中,上传到SFTP服务器,并从loacl路径中删除。我的代码是这样的 public String a(String tableName, String format,String jsondataAsString,String jsonKeyName,String userName, String password, St
public String a(String tableName, String format,String jsondataAsString,String jsonKeyName,String userName,
String password, String hostName,String remotePath) throws IOException{
userName="a";
password="a";
hostName="10.100.10.100";
remotePath="/tmp";
System.out.println("TableName-->" +tableName);
System.out.println("Format-->" +format);
System.out.println("JsonData-->" +jsondataAsString);
System.out.println("userName-->" +userName);
System.out.println("password-->" +password);
System.out.println("hostname-->" +hostName);
System.out.println("RemotePath-->" +remotePath);
String mimeType = null;
String fileName = null;
String seperator = null;
String lineSeperator = null;
boolean isFileTransferComplete=true;
OutputStream f1 =null;
if (format.equalsIgnoreCase("CSV")) {
fileName = tableName + ".csv";
mimeType = "application/CSV";
seperator = ",";
lineSeperator = "\n";
} else if (format.equalsIgnoreCase("Word")) {
fileName = tableName + ".doc";
mimeType = "application/msword";
seperator = " ";
lineSeperator = "\n\n";
} else {
fileName = tableName + ".html";
mimeType = "application/html";
seperator = " ";
lineSeperator = "<br><br>";
}
String localfilePath="D:/aaa/" +fileName;
String error="";
try{
String data = convertJsonToString(jsondataAsString, seperator, jsonKeyName,lineSeperator,format);
if (data == null || data.length() == 0) {
data = "[BLANK]";
}
boolean isFileTobeDeleted=true;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
System.out.println("Buffer-->" +buffer);
buffer.flush();
buffer.write(data.getBytes());
f1 = new FileOutputStream(localfilePath);
buffer.writeTo(f1);
isFileTransferComplete = new SFTPHandler(hostName,
PicoEmsGuiConstants.SFTP_Port, userName, password)
.uploadFile(remotePath,localfilePath);
System.out.println("FileTransfer" +isFileTransferComplete);
File target = new File(localfilePath);
target.delete();
}
System.out.println("isFileTobeDeleted" +isFileTobeDeleted);
}catch(Exception e){
System.out.println("Exception :::>" +e.getMessage());
}finally{
f1.close();
}
return isFileTransferComplete+"--"+ remotePath;
}
公共字符串a(字符串表名、字符串格式、字符串JSONDATAASTRING、字符串jsonKeyName、字符串用户名、,
字符串密码、字符串主机名、字符串远程路径)引发IOException{
userName=“a”;
密码=“a”;
hostName=“10.100.10.100”;
remotePath=“/tmp”;
System.out.println(“TableName-->”+TableName);
System.out.println(“格式-->”+格式);
System.out.println(“JsonData-->”+jsondataAsString);
System.out.println(“用户名-->”+用户名);
System.out.println(“密码-->”+密码);
System.out.println(“主机名-->”+主机名);
System.out.println(“远程路径-->”+远程路径);
字符串mimeType=null;
字符串文件名=null;
字符串分隔符=null;
字符串lineseparator=null;
布尔值isFileTransferComplete=true;
OutputStream f1=null;
if(格式为.equalsIgnoreCase(“CSV”)){
fileName=tableName+“.csv”;
mimeType=“应用程序/CSV”;
分隔符“,”;
LineSeparator=“\n”;
}else if(format.equalsIgnoreCase(“Word”)){
fileName=tableName+“.doc”;
mimeType=“应用程序/msword”;
分隔符=”;
LineSeparator=“\n\n”;
}否则{
fileName=tableName+“.html”;
mimeType=“应用程序/html”;
分隔符=”;
LineSeparator=“
”;
}
字符串localfilePath=“D:/aaa/”+文件名;
字符串错误=”;
试一试{
字符串数据=convertJsonToString(jsondataAsString,分隔符,jsonKeyName,LineSeparator,格式);
if(data==null | | data.length()==0){
data=“[BLANK]”;
}
布尔值isFileTobeDeleted=true;
ByteArrayOutputStream缓冲区=新建ByteArrayOutputStream();
System.out.println(“缓冲-->”+缓冲区);
buffer.flush();
write(data.getBytes());
f1=新的FileOutputStream(localfilePath);
缓冲区写入(f1);
isFileTransferComplete=新的SFTFandler(主机名,
PicoEmsGuiConstants.SFTP_端口、用户名、密码)
.uploadFile(远程路径、本地文件路径);
System.out.println(“FileTransfer”+isFileTransferComplete);
文件目标=新文件(localfilePath);
target.delete();
}
System.out.println(“isFileTobeDeleted”+isFileTobeDeleted);
}捕获(例外e){
System.out.println(“异常::>”+e.getMessage());
}最后{
f1.关闭();
}
返回isFileTransferComplete+“--”+远程路径;
}
我可以创建文件,但上传完成后无法从loacl路径中删除…有人能告诉我哪里出了问题吗您不需要关闭流,然后尝试删除它吗
finally {
f1.close();
if(file != null && file.exists()) {
file.delete();
}
}
您不需要关闭流,然后尝试删除它吗
finally {
f1.close();
if(file != null && file.exists()) {
file.delete();
}
}
您可能想要一个临时文件,请查看API是否支持它。为什么问题中的
f1.close()
已被删除?您可能想要一个临时文件,请查看API是否支持它。为什么问题中的f1.close()
已被删除?