文件上传如何设置允许上传格式
文件上传如何设置允许上传格式,在点击浏览时,弹出对话框中只出现符合条件的文件
[解决办法]
直接上代码:
@SuppressWarnings({"serial", "unchecked"})
public class UploadServlet extends HttpServlet {
private String uploadpath = null;
private String repository = null;
private long singlefilesize = 0;
@Override
public void init() throws ServletException {
// 获取初始化参数的值
uploadpath = getServletConfig().getInitParameter("uploadpath");
repository = getServletConfig().getInitParameter("repository");
singlefilesize = new Long(getServletConfig().getInitParameter("singlefilesize"));
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=GBK");
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("isMultipart: " + isMultipart);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(1024 * 1024 * 3);
factory.setRepository(new File(repository));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置允许上传文件的大小
upload.setFileSizeMax(singlefilesize);
String path = getServletContext().getRealPath(uploadpath);
File dirFile = new File(path);
if(!dirFile.exists()){
dirFile.mkdir();
}
try {
// Parse the request
List<FileItem> items = upload.parseRequest(request);
System.out.println(items.size());
for(FileItem item : items){
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString("GBK");
System.out.println("name: " + name);
System.out.println("value: " + value);
request.setAttribute("name", name);
request.setAttribute("value", value);
} else {
String fieldName = item.getFieldName();
[color=#99CC00]//获取文件名,截取判断下
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
System.out.println("fieldName: " + fieldName);
System.out.println("a fileName: " + fileName);
System.out.println("contentType: " + contentType);
System.out.println("isInMemory: " + isInMemory);
System.out.println("sizeInBytes: " + sizeInBytes);
request.setAttribute("fieldName", fieldName);
request.setAttribute("afileName", fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("isInMemory", isInMemory);
request.setAttribute("sizeInBytes", sizeInBytes);
fileName = fileName.substring(fileName.lastIndexOf("\") + 1);
System.out.println("b fileName: " + fileName);
request.setAttribute("bfileName", fileName);
/*fileName = FilenameUtils.getName(fileName);
System.out.println("b fileName: " + fileName);
request.setAttribute("bfileName", fileName);
String extension = FilenameUtils.getExtension(fileName);
System.out.println("extension: " + extension);*/
/*InputStream inputStream = item.getInputStream();
File saveFile = new File(dirFile, fileName);
FileOutputStream outputStream = new FileOutputStream(saveFile);
byte[] buffer = new byte[1024];
int len = 0;
while((len = inputStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();*/
File saveFile = new File(dirFile, fileName);
item.write(saveFile);
}
}
request.getRequestDispatcher("result.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[/color]
[解决办法]
图片上传格式的验证可以分前端验证和后端验证,一般来说要前端和后端都要验证,前端验证一般用js提交的时候验证后缀是否合法。