[英]Java: Overwrite the existing file on server with modified file(excel sheet) using File API
我有一些代码旨在通过覆盖主文件和覆盖可访问网络位置上的写保护副本来打开本地主文件,进行添加和保存文件。
但是我无法替换服务器上的现有文件。 我也像这样通过了stackoverflow上的其他链接,但仍然没有成功。
请帮助我! Rgds代码是
public class UploadAndSaveExcelAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
UploadAndSaveExcelForm myForm = (UploadAndSaveExcelForm)form;
String target = null;
if (myForm.getTheExcel().getFileName().length() > 0) {
FormFile myFile = myForm.getTheExcel();
System.out.println("" +myFile);
String fileName = myFile.getFileName();
byte[] fileData = myFile.getFileData();
//Get the servers upload directory real path name
String filePath = getServlet().getServletContext().getRealPath("/") +"Sheet\SparesUsed.xls";
/* Save file on the server */
//create the upload folder if not exists
File folder = new File(filePath);
if(folder.exists()){
System.out.println("Excel Sheet folder is existed therefore deleted");
folder.deleteOnExit();
}
String filePath1 = getServlet().getServletContext().getRealPath("/") +"Sheet";
File folder1 = new File(filePath1+"\\" + FileName);
System.out.println("Excel Sheet afterr delete folder is "+folder1);
boolean makedirectory=folder1.mkdir();
System.out.println(" Making Directory "+makedirectory);
if(!fileName.equals("")){
System.out.println("Server path for Excel :" +filePath);
//Create file
File fileToCreate = new File(filePath, fileName);
//If file does not exists create file
if(!fileToCreate.exists()){
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
fileOutStream.write(fileData);
fileOutStream.flush();
fileOutStream.close();
target ="success";
} return mapping.findForward(target);}
您可以使用Spoon库。
我知道距原始文章已经有一段时间了,但是看起来更易于访问的Java转换库之一似乎是Spoon( http://spoon.gforge.inria.fr/ )。
从Spoon主页( http://spoon.gforge.inria.fr/ ):
Spoon使您能够转换(请参阅下文)和分析(请参阅示例)源代码。 Spoon提供了完整且细粒度的Java元模型,可以在其中读取和修改任何程序元素(类,方法,字段,语句,表达式...)。 Spoon将作为输入源代码,并生成准备好进行编译的转换后的源代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.