Java:使用文件API使用修改过的文件(excel工作表)覆盖服务器上的现有文件
我的代码设计用于打开本地主文件,进行添加,并通过覆盖主文件和在可访问的网络位置上覆盖写保护副本来保存文件 但我无法替换服务器上的现有文件。我已经通过stackoverflow上的其他链接也像这样,但仍然没有成功 助我一臂之力!Rgds代码是Java:使用文件API使用修改过的文件(excel工作表)覆盖服务器上的现有文件,java,file,file-upload,file-io,filesystems,Java,File,File Upload,File Io,Filesystems,我的代码设计用于打开本地主文件,进行添加,并通过覆盖主文件和在可访问的网络位置上覆盖写保护副本来保存文件 但我无法替换服务器上的现有文件。我已经通过stackoverflow上的其他链接也像这样,但仍然没有成功 助我一臂之力!Rgds代码是 public class UploadAndSaveExcelAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form,
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转换库之一似乎是Spoon() 从勺子主页():
Spoon使您能够转换(见下文)和分析(见示例)源代码。Spoon提供了一个完整的、细粒度的Java元模型,其中任何程序元素(类、方法、字段、语句、表达式……)都可以访问以进行读取和修改。Spoon将源代码作为输入,并生成转换后的源代码,以备编译。Jwalin先生,对不起,我无法为您获取,Spoon库的用途是什么?我已经添加了代码,您能参考理解吗..Spoon库用于修改和替换服务器上的文件。你查过spoon图书馆的主页了吗。这里还提供了示例代码。