你是使用struts2框架的吧 struts2的上传文件,默认就是.temp的(临时文件),你不用管它,该怎么获取文件名就怎么获取文件名
保存的时候要自己重命名
String path = ServletActionContext.getServletContext().getRealPath( "/upload/"); // System.out.println(picFileName.substring(picFileName.indexOf("."))); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(pic); fos = new FileOutputStream(path + "/" + picFileName + picFileName.substring(picFileName.indexOf("."))); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) != -1) { fos.write(buffer,0, len); fos.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } pic 上传控件的名字 。picFileName 真实的文件名
import org.apache.commons.io.FilenameUtils; // apache common io String sufName = FilenameUtils.getExtension(oriName); // 得到后缀名
81,092
社区成员
341,718
社区内容
加载中
试试用AI创作助手写篇文章吧