这是我的上传文件的方法 前台是jsp的 如何限制上传文件类型和大小 10
packagecom.xiaobei.travel.utils;importjava.io.BufferedInputStream;importjava.io.Buffe...
package com.xiaobei.travel.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
public class ImageUpload {
private static final int BUFFER_SIZE = 16 * 1024;
private static void copy(File src, File dst) {
try {
System.out.println(">>>>>>>>>>>图片开始上传");
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
System.out.println("<<<<<<<<<<<图片上传成功");
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 获取后缀名
*/
public static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public static void imageupload(File myFile, String fileName,String bagName) {
File imageFileBag = new File(ServletActionContext.getServletContext()
.getRealPath("\\"+bagName+"\\" ));
if(!imageFileBag.isDirectory()){
//不是文件夹,创建即可
imageFileBag.mkdirs();
}
File imageFile = new File(ServletActionContext.getServletContext()
.getRealPath("\\"+bagName+"\\"+ fileName ));
copy(myFile, imageFile);
}
} 展开
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
public class ImageUpload {
private static final int BUFFER_SIZE = 16 * 1024;
private static void copy(File src, File dst) {
try {
System.out.println(">>>>>>>>>>>图片开始上传");
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
System.out.println("<<<<<<<<<<<图片上传成功");
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 获取后缀名
*/
public static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public static void imageupload(File myFile, String fileName,String bagName) {
File imageFileBag = new File(ServletActionContext.getServletContext()
.getRealPath("\\"+bagName+"\\" ));
if(!imageFileBag.isDirectory()){
//不是文件夹,创建即可
imageFileBag.mkdirs();
}
File imageFile = new File(ServletActionContext.getServletContext()
.getRealPath("\\"+bagName+"\\"+ fileName ));
copy(myFile, imageFile);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询