解释说明代码: String fileName = ""; try { fileName = file.getOriginalFilename(); String destFileName = "D://User/" + File.separator + fileName; System.out.println(destFileName); File destFile = new File(de
时间: 2023-06-04 17:05:25 浏览: 143
stFileName);
destFile.getParentFile().mkdirs();
file.transferTo(destFile);
System.out.println("上传成功!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("上传失败!");
}
这段代码的作用是将上传的文件保存到服务器的指定位置。首先通过file.getOriginalFilename()获取上传文件的文件名,然后组合出文件保存的完整路径,创建文件对象并创建父文件夹,最后将上传的文件保存到指定的位置。如果上传成功,则输出"上传成功!",否则输出"上传失败!"。
相关问题
Sub MergeData() Dim FilePath As String Dim FileName As String Dim SheetName As String Dim DestFileName As String Dim DestSheetName As String Dim LastRow As Long Dim DestLastRow As Long Dim i As Integer Dim j As Integer '设置源文件夹路径和目标文件夹路径 FilePath = "C:\SourceFolder" DestFilePath = "C:\DestFolder" '循环遍历源文件夹下的所有文件 FileName = Dir(FilePath) Do While FileName <> "" '打开源文件 Workbooks.Open (FilePath & FileName) '获取源文件名和表格名 SheetName = ActiveWorkbook.Sheets(1).Name '循环遍历源表格中的所有行 For i = 1 To 9405 '获取当前行的地点名称 PlaceName = ActiveWorkbook.Sheets(1).Cells(i, 1).Value '在目标文件中查找该地点名称对应的表格 DestFileName = DestFilePath & PlaceName & ".xlsx" If Dir(DestFileName) = "" Then '如果目标文件不存在,则新建该文件,并在第一行写入表头 Workbooks.Add ActiveSheet.Name = SheetName ActiveWorkbook.SaveAs DestFileName ActiveWorkbook.Close Workbooks.Open (DestFileName) DestLastRow = 1 For j = 1 To 10 ActiveWorkbook.Sheets(1).Cells(DestLastRow, j).Value = ActiveWorkbook.Sheets(1).Cells(1, j).Value Next j Else '如果目标文件已存在,则打开该文件,并在最后一行写入当前行的数据 Workbooks.Open (DestFileName) DestLastRow = ActiveWorkbook.Sheets(1).UsedRange.Rows.Count + 1 End If '将当前行的数据写入目标文件中对应的表格中 For j = 1 To 10 ActiveWorkbook.Sheets(1).Cells(DestLastRow, j).Value = ActiveWorkbook.Sheets(1).Cells(i, j).Value Next j ActiveWorkbook.Save ActiveWorkbook.Close Next i '关闭源文件 Workbooks(FileName).Close '获取下一个文件名 FileName = Dir Loop End Sub这段代码中下标越界,更改
在循环遍历源表格中的所有行之前,应该先获取源表格中的最后一行,以便在循环中使用。因此,应该将代码中的以下行:
For i = 1 To 9405
改为:
LastRow = ActiveWorkbook.Sheets(1).UsedRange.Rows.Count
For i = 2 To LastRow
这样就可以正确获取源表格中的最后一行,并避免下标越界的问题。同时,在循环中,应该将i的初始值改为2,以跳过表头行。
public int convert2Ofd(int imagefileid, String filename,String docdcsurl) { writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------start"); //String docdcsurl = Util.null2String(getPropValue("yzDcsUrl", "docdcsurl")); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------imagefileid=" + imagefileid + ";filename=" + filename + ";docdcsurl=" + docdcsurl); int newimagefileid = -1; try { if (imagefileid > 0 && !"".equals(filename) && !"".equals(docdcsurl)) { String fileext = ""; if (filename.indexOf(".") != -1) { fileext = filename.substring(filename.lastIndexOf(".")); } writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------fileext=" + fileext); String sourcefilepath = getImageFile(imagefileid + ""); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------sourcefilepath=" + sourcefilepath); if (!"".equals(sourcefilepath)) { String dcsurl = convert(docdcsurl, sourcefilepath, "29"); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------dcsurl=" + dcsurl); if (!"".equals(dcsurl)) { InputStream input = getInputStreamFromDcs(dcsurl); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------input=" + input); String newfilename = filename.substring(0, filename.lastIndexOf(".")) + ".ofd"; if (input != null) { newimagefileid = savePdfImageFile(input, newfilename); } if (new File(sourcefilepath).exists() && new File(sourcefilepath).isFile()) { new File(sourcefilepath).delete(); } } } } } catch (Exception e) { writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------Exception=" + e); } writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------newimagefileid=" + newimagefileid); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------end"); return newimagefileid; } 这段代码里2023-06-07 10:00:41,501 INFO A2 - [null] pool-20-thread-1-827[weaver.odoc.ofd.util.ConvertToPdfForDcs:395] - ConvertToPdfForDcsE9--convertUot2Ofd-------------------Exception=java.net.MalformedURLException: no protocol: {"data":{"fileHash":"f68995999879716524d33048cf89713eb6909b540af36f0c1326666747933f7f1","code":0,"destFileName":"5cf6fc7a-b466-4d2f-bd8a-90021ece7f03.ofd","srcFileName":"5cf6fc7a-b466-4d2f-bd8a-90021ece7f03.docx","srcFileSize":"1470724","destFileSize":"272921","convertType":29,"srcStoragePath":"f68995999879716524d33048cf89713eb/5cf6fc7a-b466-4d2f-bd8a-90021ece7f03.docx","destStoragePath":"2023/06/07/d897526c6b1848d38158e2d397dc1564/5cf6fc7a-b466-4d2f-bd8a-90021ece7f03.ofd","convertTime":"4224","viewUrl":"http://10.2.120.2:19090/fcscloud/view/preview/__u_3ZnLmSOahAqKi6icTcKJrPrgXJLd2m6bLJJ9Vy1AE7bOu07HXJe5EdV3ICfU4--G5-1b-C_BMkuxJ6YiThUP9cZrRTnS8WH7LNaAySY1b4J6VNRm5bUix26SO24700BVblzn6_Sgh3k8GCEv2iW5ceUlHmsE-UIfGUPobbnm9fShs4Gg-L5wSnmdcobmWgVCHNZvPMzNAj5FNM1RAAY0iGeLBNp1ezhgLFc-LsEC5fArMOLpbVonVHX-liJuUKBubUznMLCwaouob5HRqFnSwB6l-qEWFMyKeKK7yOknI8K-59iCZpKpUAzxt4hPezq1Pgh1nR1tTtwEypycLkvEnWyugeKTxu9mwyN_fFfIUyv6oV7P79BMIZt2o-B6kDJgXVaMxne2wjydgWr2nir306Hg_R0R0zS-Z4yG74g=/"},"message":"操作成功","errorcode":0}这个异常应该怎么修改
这个异常是因为在使用convert方法时,传入的docdcsurl参数格式不正确,它应该是一个有效的URL地址,但是现在它的格式是一个JSON字符串,因此Java解析它时会抛出MalformedURLException异常。
要修复这个问题,你需要将convert方法的第二个参数改为一个有效的URL地址,而不是一个JSON字符串。你可以从JSON字符串中提取出URL地址,然后将其作为convert方法的参数传入。具体的代码实现取决于JSON字符串的结构和你的需求。
阅读全文