1、文件的操作,读写文件,解决乱码问题
读文件
1
2
3
4
5
6
7
|
InputStreamReader isr =
new
InputStreamReader(
new
FileInputStream(
new
File(path)),
"UTF-8"
);
BufferedReader reader =
new
BufferedReader(isr);
String s;
while
((s = reader.readLine()) !=
null
) {
content += s +
"\n"
;
}
reader.close();
|
写文件
1
2
3
|
Writer writer =
new
BufferedWriter(
new
OutputStreamWriter(
new
FileOutputStream(newFile1.getAbsolutePath().toString()),
"UTF-8"
));
writer.write(content);
writer.close();
|
2、struts2常用标签
单选框:<s:radio name="Gender" list="#{'男':'男','女':'女'}" listKey="key" listValue="value" value="'男'" />
时间格式化:<s:date name="publishTime" format="yyyy年MM月dd日 HH:mm:ss" />
下拉框: <s:select list="#request.role" name="role1" value="roleName" key="id" headerKey="0" headerValue="清选择角色"></s:select>
3、ajax上传文件
ajax上传文件主要是使用了ajaxfileupload.js插件,ajax代码如下所示:
HTMl代码:
1
2
|
<
input
id="fileToUpload" type="file" size="20" name="myFile" class="input">
<
button
type="button" id="buttonUpload" data-dismiss="modal" class="btn btn-primary">上传</
button
>
|
js代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
$(
"#buttonUpload"
).click(
function
() {
//验证图片格式
var
format = $(
"#fileToUpload"
).val();
var
type = format.substring(format.lastIndexOf(
"."
) + 1, format.length).toLowerCase();
onsole.info(format);
console.info(type);
if
(type !=
"jpg"
&& type !=
"jpeg"
&& type !=
"bmp"
&& type !=
"gif"
&& type !=
"png"
) {
alert(
"请上传正确的图片格式"
);
return
;
}
$.ajaxFileUpload({
url :
'notice_AddImage.action'
,
//处理图片脚本
ecureuri :
false
,
fileElementId :
'fileToUpload'
,
//file控件id
dataType :
'text'
,
success :
function
(data) {
$(
"#dd"
).html(data);
var
value = $(
"#dd pre"
).html();
if
(value ==
"undefined"
|| value ==
null
) {
value = data;
}
console.info(
"dd:"
+ value);
$(
"#btn_image"
).val(value);
console.info(
"image:"
+ $(
"#btn_image"
).val());
$(
"#tooltip"
).html(
"图片导入成功"
);
},
error :
function
(data) {
$(
"#dd"
).val(data);
console.info(
"error"
);
alert(
"error"
);
}
});
});
|
java代码:
1 // myFile属性用来封装上传的文件 2 private File myFile; 3 4 // myFileContentType属性用来封装上传文件的类型 5 private String myFileContentType; 6 7 // myFileFileName属性用来封装上传文件的文件名 8 private String myFileFileName; 9 InputStream is; 10 try { 11 is = new FileInputStream(myFile); 12 // 设置上传文件目录 13 String uploadPath = TemplateUtils.BASEPATH + "\\upload"; 14 // 重命名文件 15 String fileName = StringUtils.getUUID() + this.getMyFileFileName().substring(myFileFileName.lastIndexOf("."), myFileFileName.length()); 16 // 设置目标文件 17 File toFile = new File(uploadPath, fileName); 18 // 创建一个输出流 19 OutputStream os = new FileOutputStream(toFile); 20 // 设置缓存 21 byte[] buffer = new byte[1024]; 22 int length = 0; 23 // 读取myFile文件输出到toFile文件中 24 while ((length = is.read(buffer)) > 0) { 25 os.write(buffer, 0, length); 26 } 27 // 关闭输入流 28 is.close(); 29 // 关闭输出流 30 os.close();