就先定义filename和filepath,然后你在纤禅后续的 *** 作中想要图片出现在设知稿定的某一区域中
就可以直接使用毁猛尘filename,将要上传的文件名是 its.getIPTimeStampRand()+"."+item.getName().split("\\.")[1]
基本上面几位都说啦。。。只说一点就是注意null和空的处理
import java.io.File
import java.util.HashSet
import java.util.Set
public class ReadFileSample {
private static final String PATH = "C:\\TEST"
private static final char SEPARATOR = '_'
public static void main(String[] args) {
Set<String>fileSet = new HashSet<String>()
File[] files = new File(PATH).listFiles()
for (File file : files) {
// 由于Set的元素是不重复世桥的,不用做重复判断,直接拦返渗放入
fileSet.add(getPrefixFileName(file.getName()))
}
/简脊/ 由于Set允许null,所以要去除
fileSet.remove(null)
// 打印出Set的内容(需要的数据)
System.out.println(fileSet)
}
private static String getPrefixFileName(String fileName) {
if (fileName == null || fileName.length() == 0) {
return null
}
int index = fileName.indexOf(SEPARATOR)
if (index <0) {
// 不含SEPARATOR的文件名
return null
}
// 从文件名开头到SEPARATOR的位置,截取
return fileName.substring(0, index)
}
}
1.文件上传package com.whw.action
import java.io.File
import javax.servlet.ServletContext
import org.apache.commons.io.FileUtils
import org.apache.struts2.util.ServletContextAware
import com.opensymphony.xwork2.ActionSupport
public class UploadAction extends ActionSupport implements ServletContextAware {
private static final long serialVersionUID = 1L
private File upload// 实际上传文件
private String uploadContentType// 文件的内容类型
private String uploadFileName// 文件 名称
private String fileCaption/备隐昌/ 上传文携铅件时的备注
private ServletContext context
public String execute() throws Exception {
try {
String targetDirectory = context.getRealPath("/upload")
String targetFileName = fileCaption+uploadFileName
File target = new File(targetDirectory, targetFileName)
FileUtils.copyFile(upload, target)
setUploadFileName(target.getPath())//保存文件的存仿扒放路径
} catch (Exception e) {
addActionError(e.getMessage())
return INPUT
}
return SUCCESS
}
public String getFileCaption() {
return fileCaption
}
public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption
}
public File getUpload() {
return upload
}
public void setUpload(File upload) {
this.upload = upload
}
public String getUploadContentType() {
return uploadContentType
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType
}
public String getUploadFileName() {
return uploadFileName
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName
}
public void setServletContext(ServletContext context) {
this.context = context
}
}
2.读取文件夹下的文件
package sample
import java.io.*
public class CopyFileStream{// copy one file to another file
public static void main(String args[]) {
FileInputStream fromFile=null
FileOutputStream toFile=null
try{
//生成两个已有byte文件的 流对象。
fromFile=new FileInputStream(args[0]) // get file name from console
toFile=new FileOutputStream(args[1]) // get file name from console
} catch (FileNotFoundException e){
System.err.println("File could not be found")
return//缺文件名输入,抛异常,返回。
} catch (IOException e){
System.err.println("File could not be copied ")
return
} catch (ArrayIndexOutOfBoundsException e){
System.err.println("Usage: CopyByteFile from-file to-file")
return
}
try {
fromFile.close() // close InputStream
toFile.close() // close OutputStream
}catch (IOException e){
System.err.println("Error closing File.")
}
}
}
File类下的exists()方法是判断文件是否存在返回值是boolean
希望是你需要的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)