java txt 导入,java导入txt文件并在页面展示内容

基于jfinal框架。

不一定适用,但是流程是这个流程。

各个框架的使用,有基本基础的可以自行修改。

采取的是ajaxSubmit提交,如果只是action提交到简单了,自行修改。

后台的返回可以简化。

1.前端

需要jqueryjs文件,jquery.form.js文件

代码:

html:

上传少于1500字,大小小于2M的txt文件

js:

//导入txt

$("#importfile").click(function(){

var options = {

url: '../aaa/sss',

type: 'post',

dataType: 'json',

success: function(reg) {

reg=eval("(" + reg + ")");

if(reg.data==1){

var con=reg.message;

console.log(con);

$(body).append(con);

}else{

alert(reg.message);

}

},

error: function(data) {

alert("上传错误,请稍后再试!");

}

};

$('#importform').ajaxSubmit(options);

});

2.后台代码

public void postContent() {

UploadFile uploadFile = getFile("files");

Integer loginName = getSessionAttr("loginUserName");

//        renderText(JsonKit.toJson(as.postContent(uploadFile,loginName)));

renderJson(JsonKit.toJson(as.postContent(uploadFile,loginName)));

}

/**

* 获取txt,获取内容

*/

public String postContent(UploadFile a,Integer loginName) {

String path = PathToFile.ARTICLE_PATH.replace("LOGINAME", loginName.toString());

File file=a.getFile();

Long createTime=System.currentTimeMillis();

//判断文件大小

Long size = file.length() / 1024;

String fileName = file.getName().toLowerCase();

String oldFilePath = file.getAbsolutePath();

if (null != size && size >= 2048) {

return error(ResultStr.UPFILE_OVERSIZE);

}

//判断文件类型

String fileType = fileName.substring(fileName.lastIndexOf("."));

if (!".txt".equals(fileType)) {

return error("文件类型错误");

}

//复制文件到服务器

String newFileName = oldFilePath.substring(0, oldFilePath.lastIndexOf(".")) + createTime + fileType;

File newFile = new File(newFileName);

fileName = newFile.getName();

file.renameTo(newFile);

PathToFile.getFileToDirs(newFile, path);// 把文件复制到文件夹

String filePath = path + fileName;// 文件路径

newFile.delete();// 删除上传的文件

String con=readTxt(filePath);//读取txt文本

File f = new File(filePath);

f.delete();//删除文件

if(con.length()>1500){

con=con.substring(0, 1500);

}

String content = con.replaceAll("(?i)[\ufeff]", "").replaceAll("[\u00a0]", " ").trim();

String[] split = content.split("(?

StringBuffer sb = new StringBuffer("");

for (String str : split) {

if (!StrKit.isBlank(str.replaceAll("[\u3000]", "").trim())) {

sb.append("

");

sb.append(str);

sb.append("");

}

}

return success(sb.toString());

}

private String readTxt(String pathName){

StringBuffer sb = new StringBuffer();

try {

BufferedReader br = new BufferedReader(new FileReader(pathName));

String readLine = null;

while (null != (readLine = br.readLine())) {

sb.append(readLine);

}

br.close();

return sb.toString();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return sb.toString();

}

/**

* 复制文件到文件夹

*

* @param filePath 文件路径

* @param file     需要复制的文件

*/

public static void getFileToDirs(File file, String filePath) {

File files = new File(filePath);

if (!files.exists()) {

files.mkdirs();

}

try {

FileUtils.copyFileToDirectory(file, files);

} catch (IOException e) {

e.printStackTrace();

}

}

//附读取word文件的方法

private String readDoc(String filePath){

StringBuffer sb = new StringBuffer();

String buffer = "";

try {

if (filePath.endsWith(".doc")) {

InputStream is = new FileInputStream(new File(filePath));

WordExtractor ex = new WordExtractor(is);

buffer = ex.getText();

ex.close();

if(buffer.length() > 0){

//使用回车换行符分割字符串

String [] arry = buffer.split("\\r\\n");

for (String string : arry) {

sb.append(string.trim());

}

}

} else if (filePath.endsWith(".docx")) {

OPCPackage opcPackage = POIXMLDocument.openPackage(filePath);

POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);

buffer = extractor.getText();

extractor.close();

if(buffer.length() > 0){

//使用换行符分割字符串

String [] arry = buffer.split("\\n");

for (String string : arry) {

sb.append(string.trim());

}

}

} else {

return "";

}

return sb.toString();

} catch (Exception e) {

e.printStackTrace();

return "";

}

}

3.问题

jquery string转json

var reg;

reg=eval("(" + reg + ")");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值