您的位置:首页 > 编程语言 > Java开发

spring mvc 项目中技术要点摘要

2013-02-25 09:04 183 查看
1.上传图片

jsp:

<li><input type="file" name="image" id="image" /></li>
<li><input type="button" value="保存" onclick="uploadImage();"/></li>


javascript:

function uploadImage(){
var image = document.getElementById("image").value;
if(image == "") {
alert("请选择上传的图片!");
return;
}
Ext.Ajax.request({
url: 'informationMaintenance/uploadImage?imagename='+encodeURI(image),
method:'post',
success: function (response) {
var result = response.responseText;
alert(result);
},
failure: function (response) {}
})
}


mvc controller:

//上传图片
@RequestMapping(value="/uploadImage",method=RequestMethod.POST)
public @ResponseBody String uploadImage(HttpServletRequest request,
HttpServletResponse response){
String imageName = request.getParameter("imagename").toString();
String result = "";
try {
imageName = new String(imageName.getBytes("iso-8859-1"), "utf-8");
String uploadPath =request.getSession().getServletContext().getRealPath("/")+"images/";
UploadImage ui = new UploadImage();
result = ui.saveImage(imageName, uploadPath);
} catch (UnsupportedEncodingException e) {
result = e.getMessage();
}
return result;
}


public class UploadImage {
public String saveImage(String imageName, String uploadPath){
String result = "";
try{
FileInputStream inStream = new FileInputStream(imageName);
byte[] inOutb = new byte[inStream.available()];
inStream.read(inOutb);
File outFile = new File(uploadPath + "right_index_tu_01.jpg");
FileOutputStream outStream = new FileOutputStream(outFile);
outStream.write(inOutb);
inStream.close();
outStream.close();
result = "上传成功";
}
catch(Exception e)
{
result = e.getMessage();
}
return result;
}
}


2.读取excel

sb.append("<tr><th>导入excel:</th><td><input type='file' class='inputbg_td' id='image'/></td>");
sb.append("<td><input type='button' value='上传' onclick='importExcel();'/>");


//excel导入//
function importExcel(){
var excelname = document.getElementById("image").value;
if(excelname == "") {
alert("请选择要导入的excel!");
return;
}
if(confirm('确定要导入吗?')){
Ext.Ajax.request({
url: 'fileMaintenance/importExcel?excelname='+encodeURI(excelname),
method:'post',
success: function (response) {
alert(response.responseText);
},
failure: function (response) {}
})
}
}


//导入excel
@RequestMapping(value="/importExcel",method=RequestMethod.POST)
public @ResponseBody String importExcel(HttpServletRequest request,
HttpServletResponse response){
String excelName = request.getParameter("excelname").toString();
String result = "";
try {
excelName = new String(excelName.getBytes("iso-8859-1"), "utf-8");
result = importExcelService.ImportExcel(excelName);
} catch (UnsupportedEncodingException e) {

result = e.getMessage();
}
return result;
}


public class ImportExcelService {

@Autowired
private InsertUserService insertUserService;

public String ImportExcel(String excelName){
String result = "";
OperatorExcel oe = new OperatorExcel();
Workbook rwb = oe.ReadExcel(excelName);
if(rwb == null){
result = "读取excel失败";
return result;
}
try{
Sheet rs = (Sheet) rwb.getSheet(0);
int rowNum = rs.getRows();//行
String user_id = rs.getCell(1,1).getContents(),//用户编号
quarterID = "",//小区编号
name = "",//用户名称
address = "",//用户地址
area = "",//用户面积
buildingCode = "",//幢
unitCode = "",//单元
doorplate = "",//楼层
cellphone = "",//手机
equipmentType = "",//设备类型
billing = "",//计费方式
hot_comp_type = "",//计费类别
equip_id = rs.getCell(13,1).getContents(),//采集点编号
meter_id = "",//仪表编号
pstart = "",//开通日期
collector_id = "";//集中器编号
BigDecimal  b1,b2,b3;
for(int i = 1;i < rowNum; i++){
quarterID = rs.getCell(2,i).getContents();
name = rs.getCell(3,i).getContents();
address = rs.getCell(4,i).getContents();
area = rs.getCell(5,i).getContents();
buildingCode = rs.getCell(6,i).getContents();
unitCode = rs.getCell(7,i).getContents();
doorplate = rs.getCell(8,i).getContents();
cellphone = rs.getCell(9,i).getContents();
equipmentType = rs.getCell(10,i).getContents();
billing = rs.getCell(11,i).getContents();
hot_comp_type = rs.getCell(12,i).getContents();
meter_id = rs.getCell(14,i).getContents();
pstart = rs.getCell(15,i).getContents();
collector_id = rs.getCell(16,i).getContents();
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//pstart = format.format(pstart);
String[] strs = {"",user_id,quarterID,name,address,area,buildingCode,
unitCode,doorplate,cellphone,equipmentType,billing,hot_comp_type,
equip_id,meter_id,pstart,collector_id};
insertUserService.insertUser(strs);
b1 = new BigDecimal(user_id);
b2 = new BigDecimal(equip_id);
b3 = new BigDecimal(1);
user_id = b1.add(b3).toString();//用户编号+1
equip_id = b2.add(b3).toString();//采集点编号+1
}
rwb.close();
result = "导入成功!";
}catch(Exception e){
result = e.getMessage();
}
return result;
}
}


public class OperatorExcel {
public Workbook ReadExcel(String excelName){
Workbook rwb = null;
try{
rwb = Workbook.getWorkbook(new FileInputStream(excelName));
}
catch (Exception e) {
rwb = null;
}
return rwb;
}
}


3.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: