
这个文件有几十兆,不代表整个读入,不是占用几十兆内存。先写入头部的文字,再循环读一点源文件,写一点源文件。
RandomAccessFile 也可以,只是最初要留出空间,比如一些空格
下面就直接上代码吧!当然懂得就跳过。/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.apdplat.platform.util
import java.io.*
import java.util.ArrayList
import java.util.List
/**
* 给JAVA源代码文件统一地添加licence信息头
* 检查文件package、import、类级别注释、是否有public class
* 用到了Java7的新特性,强大
* @author ysc
*/
public class AddLicenceForJavaFile {
private static int count = 0
private static List<String>fail = new ArrayList<>();
private static List<String>wrong = new ArrayList<>();
public static void main(String[] args) {
String licence="/**\n" +
" * Licensed to the Apache Software Foundation (ASF) under one or more\n" +
" * contributor license agreements. See the NOTICE file distributed with\n" +
" * this work for additional information regarding copyright ownership.\n" +
" * The ASF licenses this file to You under the Apache License, Version 2.0\n" +
" * (the \"License\"); you may not use this file except in compliance with\n" +
" * the License. You may obtain a copy of the License at\n" +
" *\n" +
" * http://www.apache.org/licenses/LICENSE-2.0\n" +
" *\n" +
" * Unless required by applicable law or agreed to in writing, software\n" +
" * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
" * See the License for the specific language governing permissions and\n" +
" * limitations under the License.\n" +
" */"
addLicenceForJavaFile(new File("D:\\Workspaces\\NetBeansProjects\\APDPlat"),licence);
System.out.println("为 "+count+" 个Java源代码文件添加licence信息头");
if(fail.size()>0){
System.out.println("处理失败个数 "+fail.size());
for(String f : fail){
System.out.println(""+f);
}
}
if(wrong.size()>0){
System.out.println("JAVA源代码错误个数 "+wrong.size());
for(String w : wrong){
System.out.println(""+w);
}
}
}
/**
* 给JAVA源代码文件统一地添加licence信息头
* @param path 源码所处的根目录
* @param licence 许可证信息(在netbeans中复制一段文本粘贴到变量的双引号内,IDE自动格式化,相当赞)
*/
private static void addLicenceForJavaFile(File path, String licence) {
if (path != null &&path.exists()) {
//处理文件夹
if (path.isDirectory()) {
String[] children = path.list();
for (int i = 0i <children.lengthi++) {
File child = new File(path.getPath() + System.getProperty("file.separator") + children[i]);
//递归处理
addLicenceForJavaFile(child, licence);
}
} else {
//处理java文件
if (path.getName()。toLowerCase()。endsWith(".java")) {
System.out.println(path.getAbsolutePath());
count++
try {
byte[] content
try (RandomAccessFile f = new RandomAccessFile(path, "rw")) {
content = new byte[ (int) f.length()]
f.readFully(content);
}
String text = new String(content);
text = text.trim();
while (text.startsWith("/n")) {
text = text.substring(1);
}
//如果已经有同样的licence,则忽略
int pos = text.indexOf(licence);
if(pos!=-1){
return
}
//有package声明的,保留package以后的内容
if (text.indexOf("package") != -1) {
text = text.substring(text.indexOf("package"));
}
//没有package声明的,有import声明的,保留import以后的内容
else if (text.indexOf("package") == -1 &&text.indexOf("import") != -1) {
text = text.substring(text.indexOf("import"));
}
//没有package声明也没有import声明的,有类级别注释的,则保留类级别注释以后的内容
else if (text.indexOf("package") == -1 &&text.indexOf("import") == -1 &&text.indexOf("/**") != -1 &&text.indexOf("public class") != -1 &&text.indexOf("/**")<text.indexOf("public class") ) {
text = text.substring(text.indexOf("/**"));
}
//没有package声明也没有import声明的,也没有类级别注释的则保留public class以后的内容
else if (text.indexOf("package") == -1 &&text.indexOf("import") == -1 &&text.indexOf("public class") != -1 &&( text.indexOf("/**")>text.indexOf("public class") || text.indexOf("/**")==-1 )) {
text = text.substring(text.indexOf("public class"));
}else{
wrong.add(path.getAbsolutePath());
return
}
try (FileWriter writer = new FileWriter(path)) {
writer.write(licence);
writer.write("\n\n");
writer.write(text);
}
}
catch (Exception ex) {
fail.add(path.getAbsolutePath());
}
}
}
}
}
}
response.setHeader()的用法
1. HTTP消息头
(1)通用信息头
即能用于请求消息中,也能用于响应信息中,但与被传输的实体内容没有关系的信息头,如Data,Pragma
主要: Cache-Control , Connection , Data , Pragma , Trailer , Transfer-Encoding , Upgrade
(2)请求头
用于在请求消息中向服务器传递附加信息,主要包括客户机可以接受的数据类型,压缩方法,语言,以及客户计算机上保留的信息和发出该请求的超链接源地址等.
主要: Accept , Accept-Encoding , Accept-Language , Host ,
(3)响应头
用于在响应消息中向客户端传递附加信息,包括服务程序的名称,要求客户端进行认证的方式,请求的资源已移动到新地址等.
主要: Location , Server , WWW-Authenticate(认证头)
(4)实体头
用做实体内容的元信息,描述了实体内容的属性,包括实体信息的类型,长度,压缩方法,最后一次修改的时间和数据的有效期等.
主要: Content-Encoding , Content-Language , Content-Length , Content-Location , Content-Type
(5)扩展头
主要:Refresh, Content-Disposition
2. 几个主要头的作用
(1)Content-Type的作用
该实体头的作用是让服务器告诉浏览器它发送的数据属于什么文件类型。
例如:当Content-Type 的值设置为text/html和text/plain时,前者会让浏览器把接收到的实体内容以HTML格式解析,后者会让浏览器以普通文本解析.
(2)Content-Disposition 的作用
当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
Content-Disposition扩展头的例子:
<%@ page pageEncoding="GBK" contentType="text/htmlcharset=utf-8" import="java.util.*,java.text.*" %><%=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.CHINA).format(new Date())
%>
<%
response.setHeader("Content-Type","video/x-msvideo")
response.setHeader("Content-Disposition", "attachmentfilename=aaa.doc")
%>
Content-Disposition中指定的类型是文件的扩展名,并且d出的下载对话框中的文件类型图片是按照文件的扩展名显示的,点保存后,文件以filename的值命名,保存类型以Content中设置的为准。
注意:在设置Content-Disposition头字段之前,一定要设置Content-Type头字段。
3.如何实现文件下载
要实现文件下载,我们只需要设置两个特殊的相应头,它们是什么头?如果文件名带中文,该如何解决?
两个特殊的相应头:
----Content-Type: application/octet-stream
----Content-Disposition: attachmentfilename=aaa.zip
例如:
response.setContentType("image/jpeg")response.setHeader("Content- Disposition","attachmentfilename=Bluehills.jpg")
如果文件中filename参数中有中文,则就会出现乱码。
解决办法:
(1)MimeUtility.encodeWord("中文.txt")//现在版本的IE还不行(2)new String("中文".getBytes("GB2312"),"ISO8859- 1")//实际上这个是错误的
4. 测试并分析文件名乱码问题
response.setHeader()下载中文文件名乱码问题
response.setHeader("Content-Disposition", "attachment filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"))response.setHeader(...)文件名中有空格的时候
String fileName = StringUtils.trim(file.getName())String formatFileName = encodingFileName(name)//在后面定义方法encodingFileName(String fileName)
response.setHeader("Content-Disposition", "attachment filename=" + formatFileName )
//处理文件名中出现的空格
//其中%20是空格在UTF-8下的编码
public static String encodingFileName(String fileName) {
String returnFileName = ""
try {
returnFileName = URLEncoder.encode(fileName, "UTF-8")
returnFileName = StringUtils.replace(returnFileName, "+", "%20")
if (returnFileName.length() > 150) {
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1")
returnFileName = StringUtils.replace(returnFileName, " ", "%20")
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace()
if (log.isWarnEnabled()) {
log.info("Don't support this encoding ...")
}
}
return returnFileName
}
一秒刷新页面一次
response.setHeader("refresh","1")二秒跳到其他页面
response.setHeader("refresh","2URL=otherPagename")没有缓存:
response.setHeader("Pragma", "No-cache")response.setHeader("Cache-Control", "no-cache")
设置过期的时间期限
response.setDateHeader("Expires", System.currentTimeMillis()+自己设置的时间期限)访问别的页面:
response.setStatus(302)response.setHeader("location","url")
通知浏览器数据采用的压缩格式:
response.setHeader("Content-Encoding","压缩后的数据")高速浏览器压缩数据的长度:
response.setHeader("Content-Length",压缩后的数据.length+"")高速浏览器图片或视频:
response.setHeader("Content-type","这个参数在tomcat里conf下的web.xml里面找")inputstream in= this.getServletContext.getResourceAsStream("/2.jpg")
int len=0
byte buffer[]= new byte[1024]
outputStream out = response.getOutputStream()
while(len=in.read(buffer)>0){
out.write(buffer,0,len)
}
高速浏览器已下载的形式:
response.setHeader("Content-disposition","attachmentfilename=2.jpg")inputstream in= this.getServletContext.getResourceAsStream("/2.jpg")
int len=0
byte buffer[]= new byte[1024]
outputStream out = response.getOutputStream()
while(len=in.read(buffer)>0){
out.write(buffer,0,len)
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)