【问题标题】:Java: Overwrite the existing file on server with modified file(excel sheet) using File APIJava:使用文件 API 用修改后的文件(excel 表)覆盖服务器上的现有文件
【发布时间】:2014-02-21 09:06:38
【问题描述】:

我的代码旨在通过覆盖主文件和覆盖可访问网络位置上的写保护副本来打开本地主文件、进行添加和保存文件。

但我无法替换服务器上的现有文件。我也像这样通过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);}

【问题讨论】:

    标签: java file file-upload file-io filesystems


    【解决方案1】:

    您可以使用 Spoon 库。

    我知道距离最初的帖子已经有一段时间了,但看起来更易于访问的 Java 转换库之一似乎是 Spoon(http://spoon.gforge.inria.fr/)。

    来自 Spoon 主页(http://spoon.gforge.inria.fr/):

    Spoon 使您能够转换(见下文)和分析(见示例)源代码。 Spoon 提供了一个完整且细粒度的 Java 元模型,其中可以访问任何程序元素(类、方法、字段、语句、表达式......)以进行读取和修改。 Spoon 将源代码作为输入并生成转换后的源代码以供编译。

    【讨论】:

    • 先生,Jwalin 对不起,我找不到你,勺子库是用来做什么的?我已经添加了代码,你可以参考它来理解..
    • Spoon 库用于修改和替换服务器上的文件。你有没有查看勺子库的主页。那里也有示例代码。
    猜你喜欢
    • 2020-09-28
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多