标签:style blog http color io os 使用 java ar
文件上传---普通文件和url文件
主要用来学习使用common-fileupload.jar和java.net.httpURLConnection
普通文件:
//上传xls文件到临时目录
if (! ServletFileUpload.isMultipartContent(request)) return;
DiskFileItemFactory factory = new DiskFileItemFactory(); // 建立factory
factory.setSizeThreshold(4*1024*1024); // 设定缓存大小
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(5120000);//5M
File srcFile = null;
try {
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField()) {
String ext = StringUtils.substringAfterLast(item.getName(), ".").toLowerCase();
//过滤文件格式
if(StringUtils.isEmpty(ext) || !"xls".equalsIgnoreCase(ext)) {
System.out.println("Unsupport file type: "+ext);
throw new Exception("Unsupport file type: "+ext);
}
String fileName = Long.toString(Calendar.getInstance().getTimeInMillis())+"."+ext;
//临时目录
String srcFilePath = "/tmp/" + fileName;
srcFile = new File(srcFilePath);
if (srcFile.exists()) {
continue;
}
item.write(srcFile);
}
}
}catch (SizeLimitExceededException e){
e.printStackTrace();
return;
}catch (FileUploadException fupEx) {
fupEx.printStackTrace();
return;
}
url文件上传:
System.out.println("Upload url file begins ");
try {
String srcURL = request.getParameter("srcURL");
if(srcURL==null || srcURL.trim().length()<=0) {
throw new Exception("No url found ");
}
String ext = StringUtils.substringAfterLast(srcURL, ".").toLowerCase();
//初级过滤文件格式
if(app.getFileExts()!=null && !app.getFileExts().contains(ext)) {
throw new Exception("Unsupport file type: "+ext);
}
String fileName = Long.toString(Calendar.getInstance().getTimeInMillis())+"."+ext;
String srcFilePath = "/tmp/" + fileName;
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File saveFile = new File(srcFilePath);
//file wall
String proxy = "192.168.1.1";
String port = "8080";
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
try{
urlfile = new URL(srcURL);
httpUrl = (HttpURLConnection)urlfile.openConnection();
httpUrl.setConnectTimeout(3000);
httpUrl.setReadTimeout(60000);
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
}catch(Exception e)
{
e.printStackTrace();
System.out.println("Souce url connect failed:"+srcURL+" by "+e.getMessage());
return;
}
try{
int count = 0;
int size = 0;
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
byte[] b = new byte[1024];
while((count=bis.read(b))!=-1) {
bos.write(b, 0, count);
size +=count;
if(size>=app.getFileSize()) {
System.out.println("File size exceeded max limit ");
return;
}
}
}catch(Exception e)
{
e.printStackTrace();
System.out.println("Save url file failed:"+srcURL+" by "+e.getMessage());
return;
}finally{
try{
bos.flush();
bis.close();
httpUrl.disconnect();
}catch(Exception e)
{
System.out.println(e.toString());
}
}
更多详细关于HttpURLConnection的学习资料请参考:
标签:style blog http color io os 使用 java ar
原文地址:http://www.cnblogs.com/ITtangtang/p/3968284.html