java对文件损坏校验

文件损坏校验

1.背景

出于项目业务的需求,需要对上传文件处理,服务端需要甄别损坏文件,去拦截。则需要对上传的文件去判断,疏于搜索水平,谷歌了下没找到好的方法。则思考了一种目前看来可行的判别方法。

2.思路。

根据文件名,通过字符串分隔,判断出文件的现用格式。再引入tika包,对文件的实际格式进行判断,则与现用格式比较,不同,则为格式强制转换等损坏文件,这样,就滤除了此类损坏的文件。

3.过程。

(1).项目里引入tika包

    <dependency>
	    <groupId>org.apache.tika</groupId>
	    <artifactId>tika</artifactId>
	    <version>1.18</version>
	    <type>pom</type>
	</dependency>
	<dependency>
	    <groupId>org.apache.tika</groupId>
	    <artifactId>tika-parsers</artifactId>
	    <version>1.18</version>
	</dependency>
	<dependency>
       <groupId>org.apache.tika</groupId>
        <artifactId>tika-core</artifactId>
        <version>1.18</version>
    </dependency>

(2).代码里使用

以下截取部分代码以作示例,具体业务,根据自身业务需要,以做变化,如有好的idea,欢迎讨论沟通。

Tika tika = new Tika();
// 标识损坏文件数
int n = 0;
// 标识损坏文件位置
int[] faultFlag = new int[multipartFiles.length];
//multipartFiles是上传的文件数组,根据自己的需求来获取真实名称,此处为我的业务场景。
String[] temp = multipartFiles[i].getOriginalFilename().split("\\.");
//获取文件现用格式
String fileType = temp[temp.length - 1];
if ("doc".equals(fileType)) {
    // 正常doc:application/x-tika-msoffice ,正常txt:text/plain
    if (!"application/x-tika-msoffice".equals(tika.detect(multipartFiles[i].getInputStream()))) {
				// 对损坏文件标识
				faultFlag[i] = 1;
				n++;
			}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
* $Id: PDFViewer.java,v 1.10 2009-08-07 23:18:33 tomoke Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ package com.sun.pdfview; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Rectangle2D; import java.awt.print.Book; import java.awt.print.PageFormat; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.awt.Toolkit; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.*; import java.io.IOException; import java.io.RandomAccessFile; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.JToolBar; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.filechooser.FileFilter; import javax.swing.SwingUtilities; import com.sun.pdfview.action.GoToAction; import com.sun.pdfview.action.PDFAction; import java.lang.reflect.InvocationTargetException; /** * A demo PDF Viewer application. */ public class PDFViewer extends JFrame implements KeyListener, TreeSelectionListener, PageChangeListener { public final static String TITLE = "SwingLabs PDF Viewer"; /** The current PDFFile */ PDFFile curFile; /** the name of the current document */ String docName; /** The split between thumbs and page */ JSplitPane split; /** The thumbnail scroll pane */ JScrollPane thumbscroll; /** The thumbnail display */ ThumbPanel thumbs; /** The page display */ PagePanel page; /** The full screen page display, or null if not in full screen mode */ PagePanel fspp; // Thread anim; /** The current page number (starts at 0), or -1 if no page */ int curpage = -1; /** the full screen button */ JToggleButton fullScreenButton; /** the current page number text field */ JTextField pageField; /** the full screen window, or null if not in full screen mode */ FullScreenWindow fullScreen; /** the root of the outline, or null if there is no outline */ OutlineNode outline = null; /** The page format for printing */ PageFormat pformat = PrinterJob.getPrinterJob().defaultPage(); /** true if the thumb panel should exist at all */ boolean doThumb = true; /** flag to indicate when a newly added document has been announced */ Flag docWaiter; /** a thread that pre-loads the next page for faster response */ PagePreparer pagePrep; /** the window containing the pdf outline, or null if one doesn't exist */ JDialog olf; /** the document menu */ JMenu docMenu; /** * utility method to get an icon from the resources of this class * @param name the name of the icon * @return the icon, or null if the icon wasn't found. */ public Icon getIcon(String name) { Icon icon = null; URL url = null; try { url = getClass().getResource(name); icon = new ImageIcon(url); if (icon == null) { System.out.println("Couldn't find " + url); } } catch (Exception e) { System.out.println("Couldn't find " + getClass().getName() + "/" + name); e.printStackTrace(); } return icon; } /// FILE MENU Action openAction = new AbstractAction("Open...") { public void actionPerformed(ActionEvent evt) { doOpen(); } }; Action pageSetupAction = new AbstractAction("Page setup...") { public void actionPerformed(ActionEvent evt) { doPageSetup(); } }; Action printAction = new AbstractAction("Print...", getIcon("gfx/print.gif")) { public void actionPerformed(ActionEvent evt) { doPrint(); } }; Action closeAction = new AbstractAction("Close") { public void actionPerformed(ActionEvent evt) { doClose(); } }; Action quitAction = new AbstractAction("Quit") { public void actionPerformed(ActionEvent evt) { doQuit(); } }; class ZoomAction extends AbstractAction { double zoomfactor = 1.0; public ZoomAction(String name, double factor) { super(name); zoomfactor = factor; } public ZoomAction(String name, Icon icon, double factor) { super(name, icon); zoomfactor = factor; } public void actionPerformed(ActionEvent evt) { doZoom(zoomfactor); } } ZoomAction zoomInAction = new ZoomAction("Zoom in", getIcon("gfx/zoomin.gif"), 2.0); ZoomAction zoomOutAction = new ZoomAction("Zoom out", getIcon("gfx/zoomout.gif"), 0.5); Action zoomToolAction = new AbstractAction("", getIcon("gfx/zoom.gif")) { public void actionPerformed(ActionEvent evt) { doZoomTool(); } }; Action fitInWindowAction = new AbstractAction("Fit in window", getIcon("gfx/fit.gif")) { public void actionPerformed(ActionEvent evt) { doFitInWindow(); } }; class ThumbAction extends AbstractAction implements PropertyChangeListener { boolean isOpen = true; public ThumbAction() { super("Hide thumbnails"); } public void propertyChange(PropertyChangeEvent evt) { int v = ((Integer) evt.getNewValue()).intValue(); if (v <= 1) { isOpen = false; putValue(ACTION_COMMAND_KEY, "Show thumbnails"); putValue(NAME, "Show thumbnails"); } else { isOpen = true; putValue(ACTION_COMMAND_KEY, "Hide thumbnails"); putValue(NAME, "Hide thumbnails"); } } public void actionPerformed(ActionEvent evt) { doThumbs(!isOpen); } } ThumbAction thumbAction = new ThumbAction(); Action fullScreenAction = new AbstractAction("Full screen", getIcon("gfx/fullscrn.gif")) { public void actionPerformed(ActionEvent evt) { doFullScreen((evt.getModifiers() & evt.SHIFT_MASK) != 0); } }; Action nextAction = new AbstractAction("Next", getIcon("gfx/next.gif")) { public void actionPerformed(ActionEvent evt) { doNext(); } }; Action firstAction = new AbstractAction("First", getIcon("gfx/first.gif")) { public void actionPerformed(ActionEvent evt) { doFirst(); } }; Action lastAction = new AbstractAction("Last", getIcon("gfx/last.gif")) { public void actionPerformed(ActionEvent evt) { doLast(); } }; Action prevAction = new AbstractAction("Prev", getIcon("gfx/prev.gif")) { public void actionPerformed(ActionEvent evt) { doPrev(); } }; /** * Create a new PDFViewer based on a user, with or without a thumbnail * panel. * @param useThumbs true if the thumb panel should exist, false if not. */ public PDFViewer(boolean useThumbs) { super(TITLE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { doQuit(); } }); doThumb = useThumbs; init(); } /** * Initialize this PDFViewer by creating the GUI. */ protected void init() { page = new PagePanel(); page.addKeyListener(this); if (doThumb) { split = new JSplitPane(split.HORIZONTAL_SPLIT); split.addPropertyChangeListener(split.DIVIDER_LOCATION_PROPERTY, thumbAction); split.setOneTouchExpandable(true); thumbs = new ThumbPanel(null); thumbscroll = new JScrollPane(thumbs, thumbscroll.VERTICAL_SCROLLBAR_ALWAYS, thumbscroll.HORIZONTAL_SCROLLBAR_NEVER); split.setLeftComponent(thumbscroll); split.setRightComponent(page); getContentPane().add(split, BorderLayout.CENTER); } else { getContentPane().add(page, BorderLayout.CENTER); } JToolBar toolbar = new JToolBar(); toolbar.setFloatable(false); JButton jb; jb = new JButton(firstAction); jb.setText(""); toolbar.add(jb); jb = new JButton(prevAction); jb.setText(""); toolbar.add(jb); pageField = new JTextField("-", 3); // pageField.setEnabled(false); pageField.setMaximumSize(new Dimension(45, 32)); pageField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { doPageTyped(); } }); toolbar.add(pageField); jb = new JButton(nextAction); jb.setText(""); toolbar.add(jb); jb = new JButton(lastAction); jb.setText(""); toolbar.add(jb); toolbar.add(Box.createHorizontalGlue()); fullScreenButton = new JToggleButton(fullScreenAction); fullScreenButton.setText(""); toolbar.add(fullScreenButton); fullScreenButton.setEnabled(true); toolbar.add(Box.createHorizontalGlue()); JToggleButton jtb; ButtonGroup bg = new ButtonGroup(); jtb = new JToggleButton(zoomToolAction); jtb.setText(""); bg.add(jtb); toolbar.add(jtb); jtb = new JToggleButton(fitInWindowAction); jtb.setText(""); bg.add(jtb); jtb.setSelected(true); toolbar.add(jtb); toolbar.add(Box.createHorizontalGlue()); jb = new JButton(printAction); jb.setText(""); toolbar.add(jb); getContentPane().add(toolbar, BorderLayout.NORTH); JMenuBar mb = new JMenuBar(); JMenu file = new JMenu("File"); file.add(openAction); file.add(closeAction); file.addSeparator(); file.add(pageSetupAction); file.add(printAction); file.addSeparator(); file.add(quitAction); mb.add(file); JMenu view = new JMenu("View"); JMenu zoom = new JMenu("Zoom"); zoom.add(zoomInAction); zoom.add(zoomOutAction); zoom.add(fitInWindowAction); zoom.setEnabled(false); view.add(zoom); view.add(fullScreenAction); if (doThumb) { view.addSeparator(); view.add(thumbAction); } mb.add(view); setJMenuBar(mb); setEnabling(); pack(); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width - getWidth()) / 2; int y = (screen.height - getHeight()) / 2; setLocation(x, y); if (SwingUtilities.isEventDispatchThread()) { setVisible(true); } else { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { setVisible(true); } }); } catch (InvocationTargetException ie) { // ignore } catch (InterruptedException ie) { // ignore } } } /** * Changes the displayed page, desyncing if we're not on the * same page as a presenter.
校验Java Excel文件是否损坏可以使用Apache POI库来实现。可以使用以下代码来校验Excel文件是否损坏: ```java import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelValidator { public static boolean isExcelFileValid(String filePath) { try { Workbook workbook = WorkbookFactory.create(new File(filePath)); // 如果没有抛出异常,则表示Excel文件有效 return true; } catch (Exception e) { // 如果抛出异常,则表示Excel文件损坏 return false; } } } ``` 你可以调用`isExcelFileValid`方法并传入Excel文件的路径来校验文件是否损坏。如果返回`true`,则表示文件有效;如果返回`false`,则表示文件损坏。请注意,这个方法使用了Apache POI库来读取Excel文件,所以你需要在项目中添加相应的依赖。 希望这个回答对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [Java之Excel导出工具类使用教程](https://blog.csdn.net/x541211190/article/details/88694568)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [java导出excel表格进行判断和时间日期格式设置](https://blog.csdn.net/qq_45866386/article/details/120427347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值