📄 fileupload.java
字号:
package com.trisun.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* @author qjs
* @Date : 2006-09-21
* Description:
* 用于上传文件的servlet,
* 参数有:
* type: 子系统的类型
* path: 保存文件的相对地址
* rewritable: 1=可重写文件。 0-如果文件已经存在则出错。
* -----------------
*
*/
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
private int maxPostSize = 100 * 1024 * 1024;
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html; charset=UTF-8");
req.setCharacterEncoding("UTF-8");
//读入图片的目录信息
String uploadPath =null;
Properties fileprops = new Properties();
fileprops.load(new FileInputStream(PropsUtil.getWebPath()+ ConfigInfo.propFile_common));
uploadPath = fileprops.getProperty(ConfigInfo.propName_upload);
//读入指定的子目录信息
String typePath = req.getParameter("type");
if ((typePath!=null) && typePath.length()>0)
{
uploadPath = uploadPath + typePath +"\\";
}
if (typePath!=null) System.out.println("typePath out");
String subPath = req.getParameter("path");
if ((subPath!=null) && subPath.length()>0){
uploadPath = uploadPath + subPath +"\\";
}
boolean isRewritable=(req.getParameter("rewritable").equals("1")?true:false);
String fileName = req.getParameter("fileName");
System.out.println(" full path "+ uploadPath);
try
{
File file = new File(uploadPath);
System.out.println("uploadPath's file: getAbsolutePath="+ file.getAbsolutePath() +" path="+ file.getPath());
if(!file.exists()) file.mkdirs();
}
catch(Exception ex)
{
System.out.println("建立子目录失败");
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxPostSize);
try {
List fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
System.out.println("uploadPath=" + uploadPath + ",oldFileName=" + name + ",newFileName=" + fileName);
File file = rename(name,fileName);
try {
//item.write(file);
InputStream stream=item.getInputStream();// 把文件读入
OutputStream bos=new FileOutputStream(uploadPath + fileName);
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=stream.read(buffer,0,8192))!=-1){
bos.write(buffer,0,bytesRead);// 将文件写入服务器
}
bos.close();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
/*try {
File file=new File(uploadPath + name);
if(file.exists()&&(isRewritable==false)){
throw new IOException("文件名已经存在!");
}
String[] arr = {};
if(!name.trim().equals("")){
arr = name.split(".");
}
System.out.println("uploadPath=" + uploadPath + ",fileName=" + str + ",fileType=" + arr[1]);
item.write(new File(uploadPath + str + "." + arr[1]));
}catch (IOException e){
throw e;
}
catch (Exception e) {
e.printStackTrace();
}*/
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
private File rename(String oldName,String newName){
int dot=oldName.lastIndexOf(".");
String ext=null;
if(dot!=-1){
ext=oldName.substring(dot); // includes "."
}
File f=new File(newName+ext);
createNewFile(f);
return f;
}
private boolean createNewFile(File f) {
try {
return f.createNewFile();
} catch (IOException ignored) {
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -